0

I am new to php and am having a problem in connecting a database to a page being used to book appointments. Can anybody advise me on this code and error?

$mysqli = new mysqli('host', 'user', 'pass', 'dbname');
$stmt = $mysqli->prepare("select * from bookings where MONTH(date) = ? AND YEAR(date) = ?");
$stmt->bind_param('ss', $month, $year);
$bookings = array();

if($stmt->execute()){
  $result = $stmt->bind_result($month, $year);
  if($result->num_rows>0){
    while($row = $result->fetch()){
      $bookings[] = $row['date'];
    }
    $stmt->close();
  }
}

I am getting these errors:

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in /var/www/vhosts/222/1112657/webspace/httpdocs/menugeek.ie/booking-app/index.php on line 17

Notice: Trying to get property 'num_rows' of non-object in /var/www/vhosts/222/1112657/webspace/httpdocs/menugeek.ie/booking-app/index.php on line 18

Screenshots below of how my table 'bookings' in my database is set up. Thanks! Database screenshot 1 Database screenshot 2

  • 2
    `select *` selects __all fields__ from db. I doubt you have only two fields there. That's why in `bind_result` you __must__ assign all fields to variables. – u_mulder Jul 13 '20 at 10:01
  • https://stackoverflow.com/questions/18753262/example-of-how-to-use-bind-result-vs-get-result may give some examples of what you should be doing. – Nigel Ren Jul 13 '20 at 10:03

0 Answers0