php - 为什么 mysqli 给出 "Commands out of sync"错误?

标签 php mysql mysqli

我正在尝试运行以下内容。

<?php

$db = mysqli_connect("localhost","user","pw") or die("Database error");
mysqli_select_db($db, "database");

$agtid = $_POST['level'];

$sql = sprintf("call agent_hier(%d)", $agtid);

$result = mysqli_query($db, $sql) or exit(mysqli_error($db));

if ($result) {
    echo "<table border='1'>
        <tr><th>id</th>
        <th>name</th>
        <th>parent_id</th>
        <th>parent_name</th>
        <th>level</th>
        <th>email</th></tr>";

    while ($row = mysqli_fetch_assoc($result)) 
    {
        $aid = $row["id"];
        $sql2 = "SELECT * FROM members WHERE MEMNO = '$aid'";
        $result2 = mysqli_query($db,$sql2) or exit(mysqli_error($db));

            while ($newArray = mysqli_fetch_array($result2)) {
                $fname = $newArray['FNAME'];
                $lname = $newArray['LNAME'];
                $mi = $newArray['MI'];  
                $address = $newArray['ADDRESS'];    
                $city = $newArray['CITY'];  
                $state = $newArray['STATE'];    
                $zip = $newArray['ZIP'];
                            $kdate = $newArray['KDATE'];
                $date = abs(strtotime(date('m/d/Y')) - strtotime(date($kdate))) / (60 * 60 * 24);
            }

        echo sprintf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
            $row["id"],$row["name"],
            $row["parent_id"],$row["parent_name"],
            $row["level"],$row["email"]);
    }

    echo "</table>";
}

mysqli_free_result($result);
mysqli_close($db);

?>

如果我从以下位置删除行:

  $aid = $row["agent_id"];

到....

  $date = abs(strtotime(date('m/d/Y')) - strtotime(date($kdate))) / (60 * 60 * 24);
  }

一切都会好起来的。如果没有,我会收到以下错误:

Commands out of sync; you can't run this command now

在研究中,我认为这可能是由于多个 MySQLi 查询同时运行,其中使用 mysqli_multi_query 但对于 the guide 中的所有样本和一般数据似乎不适用。

有什么想法吗?

最佳答案

MySQL 客户端不允许您执行新查询,其中仍有行要从正在进行的查询中获取。见 Commands out of sync在 MySQL 文档中关于常见错误。

您可以使用 mysqli_store_result()从外部查询中预取所有行。这将在 MySQL 客户端中缓冲它们,因此从服务器的角度来看,您的应用程序已经获取了完整的结果集。然后,即使在从现在缓冲的外部结果集中获取行的循环中,您也可以执行更多查询。

或者你mysqli_result::fetch_all()它将完整的结果集作为 PHP 数组返回,然后您可以循环该数组。

调用存储过程是一种特殊情况,因为存储过程有可能返回多个结果集,每个结果集都可能有自己的行集。这就是@a1ex07 的答案提到使用 mysqli_multi_query() 并循环直到 mysqli_next_result() 没有更多结果集的原因。这是满足 MySQL 协议(protocol)所必需的,即使在您的情况下,您的存储过程只有一个结果集。


PS:顺便说一句,我看到您正在执行嵌套查询,因为您有表示层次结构的数据。您可能需要考虑以不同的方式存储数据,以便更轻松地查询它。我做了一个题为 Models for Hierarchical Data with SQL and PHP 的演讲。 .我还在我的书 SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming 的一章中讨论了这个主题.


下面是如何在CodeIgnitor 3.0.3中实现mysqli_next_result():

system/database/drivers/mysqli/mysqli_driver.php第262行修改

protected function _execute($sql)
{
    return $this->conn_id->query($this->_prep_query($sql));
}

到这里

protected function _execute($sql)
{
    $results = $this->conn_id->query($this->_prep_query($sql));
    @mysqli_next_result($this->conn_id); // Fix 'command out of sync' error
    return $results;
}

这是自 2.x 以来一直存在的问题。我刚刚更新到 3.x,不得不将这个 hack 复制到新版本。

关于php - 为什么 mysqli 给出 "Commands out of sync"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3632075/

相关文章:

php 正则表达式,末尾带有可选字符

php - 不能赋负值

mysql - mysql中的递归选择

php - 解析错误 : syntax error, unexpected T_ELSE, expecting T_FUNCTION in/home/petit/public_html/system/database/mysqli.php on line 54

php - 多个 MySql 插入和更新未按预期运行

php - 登录页面和注册页面之间的连接

php - 如何在 Shopware 6 中进行模板多重继承?

mysql - 合并具有不同ID但相同数据的MySQL行

php - 值已经存在于 mysql 数据库和 php 中

php - anahkiasen/用于 Lumen/(Laravel 微框架)的前表单生成器库