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


Successive queries won't run unless I close the mysqli connection

我有 2 个查询必须运行:

  1. 存储过程中的复杂 SELECT 查询,用于标识需要修改的记录。
  2. 使用第一个记录 ID 的 UPDATE,用于修改同一表中的值。

尝试从第一个关闭中获取结果,以便我可以运行第二个,但是我唯一可以这样做而不会收到此错误的方法:

命令

不同步;您现在无法运行此命令

是关闭 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);

我已经尝试了我找到的所有建议(包括来自堆栈溢出帖子的十几个(,但没有任何效果。 我错过了什么? 必须这样做似乎不合理,特别是考虑到我可以使用"free_result"语句连续运行其他查询。

它是由已知的"内部SQL错误"引起的,其中命令不同步。因此,必须关闭并重新打开连接才能使命令恢复同步。

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