除非我关闭mysqli连接,否则连续查询都不会运行

时间:2014-12-23 02:44:11

标签: php mysql linux mysqli

我有两个必须运行的查询:

  1. 存储过程中的复杂SELECT查询,用于标识需要修改的记录。
  2. 使用第一个修改同一表中的值的记录ID来更新。
  3. 我尝试从第一个结果得到结果,所以我可以运行第二个,但唯一的方法是我可以这样做而不会出现这个错误:

      

    命令不同步;你现在不能运行这个命令

    关闭mysqli连接并重新创建它。

    这是我的代码的本质:

    //  The $sql is to run a stored procedure.  I omitted that code
    //  because it's very lengthy, but it works perfectly every time.
    //  The stored procedure is basically a recursive SELECT query to
    //  pull hierarchical data from a self-referencing data set.
    
    $res = mysqli_query($mysqli, $sql);
    
    //  Build a comma-delimited list of id numbers
    $ids = "";
    $i = 0;
    $qty = mysqli_num_rows($res);
    while ($recs = mysqli_fetch_array($res)) {
      $i += 1;
      $ids .= $recs["data_id"];
      if ($i < $qty) $ids .= ", ";
    }
    mysqli_free_result($res);
    
    //  Close connection to MySQL (Remove this & connect statement below causes it to fail)
    mysqli_close($mysqli);
    
    //  Re-open connection to MySQL (Remove this & close statement above causes it to fail)
    $mysqli = connect_DB();
    
    $sql = "UPDATE data SET data_val = data_val + ".$val." WHERE data_id IN (".$ids.")";
    
    $res = mysqli_query($mysqli, $sql);
    

    我已经尝试了我发现的所有建议(包括来自stackoverflow帖子的十几个),但没有任何效果。我错过了什么?这样做肯定是不合理的,特别是考虑到我可以通过使用&#34; free_result&#34;来反复运行其他查询。言。

1 个答案:

答案 0 :(得分:0)

这是由已知的“内部SQL Bug”引起的,其中命令不同步。因此,必须关闭并重新打开连接才能使命令同步。

用户贡献说明中的参考: http://php.net/manual/en/mysqli-result.free.php

相关问题