2015-10-22 37 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.

相關問題