PHP:如果调用提供结果的存储过程后再次使用mysqli :: query(),则“命令不同步" [英] PHP: «Commands out of sync» if I mysqli::query() again after a call to a results-giving stored procedure
问题描述
我的数据库中有一个存储过程,该存储过程返回表中的所有记录:
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>";
}
值得注意的是,如果我交换了两个查询(即,我在最后调用了存储过程),则它可以正常工作而没有任何错误.在第二个查询之前关闭close()结果无济于事. 一些提示吗?
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».
推荐答案
关于phpinet/mysqli.query的手动条目的注释已更新,现在包括有关此问题的答案.总而言之,在$ 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/zh/mysqli.query.php#102904
这篇关于PHP:如果调用提供结果的存储过程后再次使用mysqli :: query(),则“命令不同步"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!