【问题标题】:PHP: «Commands out of sync» if I mysqli::query() again after a call to a results-giving stored procedurePHP:如果我在调用结果存储过程后再次使用 mysqli::query(),则“命令不同步”
【发布时间】:2011-02-14 21:41:16
【问题描述】:

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

CREATE PROCEDURE showAll()
BEGIN
    SELECT * FROM myTable;
END

SP 按预期工作。但是,如果我在 php 脚本中调用它,然后尝试再次查询数据库,它总是会失败:

// $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>";
}

值得注意的是,如果我交换两个查询(即我最后调用存储过程),它可以正常工作而不会出现任何错误。在第二个查询之前关闭()结果没有帮助。 一些提示?

编辑:mysqli::error() 是:«命令不同步;您现在无法运行此命令»。

【问题讨论】:

  • 如果你调用 $t->free(); 会发生什么在您处理完第一个查询之后?
  • 你检查过mysqli::error吗?
  • 我在第一个 while 循环之后放了一个 $t->free() 但行为没有改变。 mysqli::error 是:“命令不同步;您现在无法运行此命令”。
  • 你为什么不直接使用SELECT * FROM myTable;?????
  • 那个 SP 只是一个例子。实际情况更复杂,我更喜欢使用 SP 来保持数据完整性。

标签: php stored-procedures mysqli


【解决方案1】:

mysqli.query 的 php.net/manual 条目上的 cmets 已更新,现在包含对此的答案。总而言之,在 $t->close() 之后调用 $mysqli->next_result()。向 petrus.jvr 致敬!

链接:http://www.php.net/manual/en/mysqli.query.php#102904

【讨论】:

    【解决方案2】:

    @jymian,你的答案不清楚。请表达清楚。

    使用$t-&gt;free() 释放结果集后,

    致电$mysqli-&gt;next_result()

    上述调用将为下一个结果集做准备,您不会出现同步错误。

    【讨论】:

      猜你喜欢
      • 2016-11-12
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2010-12-13
      • 2016-01-23
      • 2012-01-03
      • 2020-12-24
      相关资源
      最近更新 更多