Search code examples
phpmysqliprepared-statement

Call to a member function bind_param() on boolean ,mysqli_stmt::store_result and mysqli_stmt::close()


here is my code... it's about using php mysqli extension

<?php
error_reporting(E_ALL);
$db = new mysqli("localhost","root","","dee");

if ($db->connect_errno)
{
    die('Unable to connect to database');
}
mysqli_set_charset($db,"utf8");


$storeid=4;
$categoryid=6; 

$statement_store = $db->prepare('SELECT * FROM tbl_store WHERE store_id=?');
$statement_store->bind_param('i',$storeid);
$statement_store->execute();

$statement_store->store_result();//---------------(1)

$statement_store->bind_result($store_id,$store_name,$store_description,$store_image,$store_open,$store_close,$store_foldername);
$statement_store->fetch();
$store = $store_name;

//$statement_store->close();//--------------(2)


$statement_category = $db->prepare('SELECT * FROM tbl_category WHERE category_id=?');
$statement_category->bind_param('i',$categoryid);
$statement_category->execute();
$statement_category->bind_result($category_id,$category_name);
$statement_category->fetch();
$category = $category_name;

echo $store;
echo '<br>';
echo $category;

?>
  1. Fatal error: Call to a member function bind_param() on boolean error gives when not using both (1) and (2)
  2. when use (1) or (2) not gives error
  3. when use both (1) and (2) not gives error

can anybody tell me what was happen here ?


Solution

  • When you don't use store_result() or close() then your first prepared statement (or the result of it) is still "active". This means you must read the data somehow before you can issue a new prepared statement. Because of that your second prepare() statement will fail, it returns the boolean value false.

    Check the $db->error field and you will see you get the "Commands out of sync; you can't run this command now" error message. From the MySQL manual B.5.2.14 Commands out of sync:

    If you get Commands out of sync; you can't run this command now in your client code, you are calling client functions in the wrong order.

    This can happen, for example, if you are using mysql_use_result() and try to execute a new query before you have called mysql_free_result(). It can also happen if you try to execute two queries that return data without calling mysql_use_result() or mysql_store_result() in between.