【问题标题】:"Commands out of sync; you can't run this command now" - Caused by mysqli::multi_query“命令不同步;您现在无法运行此命令” - 由 mysqli::multi_query 引起
【发布时间】:2011-06-15 15:05:54
【问题描述】:

我正在通过mysqli::multi_query 运行多次删除,它正在搞乱下一个查询。抛出以下错误。

Error - SQLSTATE HY000. 
Sql error: Commands out of sync; you can't run this command now

我是否需要以某种方式清除多重查询,以免与我的下一个查询混淆?这个错误的原因是什么?

这就是我运行多重查询的方式:

function deleteSomeTables($args) {
    $sql = 'delete 1;delete another;';
    if ($database->multi_query($sql)) {
        return true;
    } else {
        return false;
    }
}

我在 Windows 7 上使用最新版本的 Xampp

【问题讨论】:

    标签: php mysqli mysqli-multi-query


    【解决方案1】:

    Seva 的代码很可能会解决您的问题,如果您像我一样使用程序风格,请使用此

    do {
      if ($result = mysqli_store_result($connect)) {
        mysqli_free_result($result);
      }
    } while (mysqli_next_result($connect));
    

    【讨论】:

      【解决方案2】:

      这帮助我消除了“命令不同步”错误:

      do { 
          $mysqli_conn_obj->use_result(); 
      }while( $mysqli_conn_obj->more_results() && $mysqli_conn_obj->next_result() );
      

      在调用multi_query后添加此代码,它将使用结果集并解决错误。

      【讨论】:

        【解决方案3】:

        通过使用mysqli::multi_query,您正在触发查询,但您需要在继续之前处理这些查询的结果。 documentation page 描述了处理结果的各种方法。处理后,您将能够正常执行其他查询。

        您遇到的错误消息实际上在 mysqli::query 的页面上得到了更好的解释(尽管请记住,mysqli::query 在这种情况下不会返回结果对象,因为您正在执行删除操作)。

        您当然可以将 multi_query 更改为多个单个查询,我不知道每种方法的优缺点是什么。

        【讨论】:

        • 那么我如何清除查询,因为我没有得到任何返回。你实际上并没有告诉我任何我不知道的事情。
        • 类似... do { $mysqli->use_result(); } while( $mysqli->next_result() )
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-19
        • 1970-01-01
        • 2013-12-27
        • 2019-07-04
        • 2012-12-01
        相关资源
        最近更新 更多