2015-10-22 38 views
3

这里是我的代码...它是关于使用PHP mysqli扩展调用一个成员函数bind_param()布尔,mysqli_stmt :: store_result和mysqli_stmt :: close()方法

<?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. 致命错误:调用一个成员函数bind_param()上布尔错误不使用时给出 两者(1)和(2)
  2. 当使用(1)或(2)不给出错误
  3. 当同时使用(1)和(2)不会给出错误

有人可以告诉我这里发生了什么吗?

回答

2

当您不使用store_result()close()时,您的第一个准备好的语句(或其结果)仍然是“活动的”。这意味着您必须以某种方式读取数据,然后才能发出新的预备声明。因为你的第二个prepare()语句将失败,它返回布尔值false

检查$db->error字段,你会看到你得到“命令不同步;你现在不能运行这个命令”的错误信息。来自MySQL手册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.

相关问题