問題描述
我的數據庫中有一個存儲過程,它返回表中的所有記錄:
I have a stored procedure in my db that returns all records in a table:
CREATE PROCEDURE showAll()
BEGIN
SELECT * FROM myTable;
END
SP 工作正常.但是,如果我在 php 腳本中調用它然后我再次嘗試查詢數據庫,它總是失敗:
The SP works just as expected. But, if I call it in a php script and then I try to query the database again, it always fail:
// $mysqli is a db connection
// first query:
if (!$t = $mysqli->query("call showAll()"))
die('Error in the 1st query');
while ($r = $t->fetch_row()) {
echo $r[0] . "<br>"; // this is ok
}
$t->free(); // EDIT (this doesn't help anyway)
// second query (does the same thing):
if (!$t = $mysqli->query("SELECT * from myTable"))
die('Error in the 2nd query'); // I always get this error
while ($r = $t->fetch_row()) {
echo $r[0] . "<br>";
}
值得注意的是,如果我交換兩個查詢(即我在最后調用存儲過程),它可以正常工作而不會出現任何錯誤.在第二個查詢之前關閉()結果沒有幫助.一些提示?
Notable, if I swap the two queries (i.e. I call the stored procedure at the end) it works without any error. To close() the result before the second query doesn't help. Some hints?
mysqli::error() 是:?命令不同步;您現在無法運行此命令?.
mysqli::error() is: ?Commands out of sync; you can't run this command now?.
推薦答案
mysqli.query 的 php.net/manual 條目的評論已更新,現在包含對此的答案.總而言之,在 $t->close() 之后調用 $mysqli->next_result().向 petrus.jvr 致敬!
The comments on the php.net/manual entry for mysqli.query have been updated, and now include an answer for this. To summarize, call $mysqli->next_result() after $t->close(). Kudos to petrus.jvr!
鏈接:http://www.php.net/manual/en/mysqli.query.php#102904
這篇關于PHP:“命令不同步"如果我在調用結果存儲過程后再次使用 mysqli::query()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!