【问题标题】:Commands out of sync; you can't run this command now; PHP While loop命令不同步;您现在无法运行此命令; PHP While 循环
【发布时间】:2014-05-19 01:41:55
【问题描述】:

当运行下面的代码时,在第二个循环中(它循环的次数)它给了我一个命令不同步错误。这是什么原因造成的?

PHP 代码:

while ($i < $quantity) {
    $result = mysqli_multi_query($con, "
        INSERT INTO parts (capacity, length, width, height, orientation, weight, location, description, status, partNumber) VALUES ('$capacity', '$length', '$width', '$height', '$orient', '$weight', '$location', '$desc', '$status', '$partNum');

        INSERT INTO craneparts (craneID, partsID) VALUES ('$craneID', LAST_INSERT_ID());
    ") or die(mysqli_error($con)); 
    $i++;
}

【问题讨论】:

  • 哇!不知道有mysqli_multi_query...要学的东西太多
  • 我知道!我今天早些时候才知道。在我遇到需要它之前,我什至不会考虑这种可能性。

标签: php mysql


【解决方案1】:

https://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

如果您的命令不同步;你现在不能运行这个命令 您的客户端代码,您以错误的顺序调用客户端函数。

这可能发生,例如,如果您使用 mysql_use_result() 并且 在调用 mysql_free_result() 之前尝试执行新查询。 如果您尝试执行两个返回数据的查询,也会发生这种情况 中间不调用 mysql_use_result() 或 mysql_store_result()。

尝试在 mysqli_multi_query 调用之间调用 mysqli_next_result

while ($i < $quantity) {
    $result = mysqli_multi_query($con, "
        INSERT INTO parts (capacity, length, width, height, orientation, weight, location, description, status, partNumber) VALUES ('$capacity', '$length', '$width', '$height', '$orient', '$weight', '$location', '$desc', '$status', '$partNum');

        INSERT INTO craneparts (craneID, partsID) VALUES ('$craneID', LAST_INSERT_ID());
    ") or die(mysqli_error($con));

    while(mysqli_more_results($con) && mysqli_next_result($con)) {;} // flush multi_queries

    $i++;
}

【讨论】:

  • 收到此错误mysqli_free_result() expects parameter 1 to be mysqli_result
  • @LiggyRide 看起来 mysqli_free_result 不是正确的方法,因为 mysqli_multi_query 不返回结果对象。你能用 mysqli_next_result() 再试一次吗?
  • mysqli_next_result() expects parameter 1 to be mysqli, boolean given 给定的布尔值也与 mysqli_free_result() 中的相同,只需删除该部分即可。
  • 尝试使用您编辑帖子的代码,抱歉没有先阅读。
  • 给我这个回复:Strict Standards: mysqli_next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method 但它有效。这可以吗?还是需要更多故障排除?
猜你喜欢
  • 1970-01-01
  • 2019-07-04
  • 1970-01-01
  • 2012-07-19
  • 1970-01-01
  • 2013-12-27
  • 2012-12-01
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多