本文介绍了PHP:“命令不同步"如果我在调用结果存储过程后再次使用 mysqli::query()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有一个存储过程,它返回表中的所有记录:

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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 13:16