1

我正在尝试在下面的代码中使用 mysqli_multi_query 函数,但遇到此错误:

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

// add call stats to incoming nodes
        $sql = "INSERT INTO `" . node_name_formatter($i_route) . "` ";
        $sql .= "            (`timeperiod`, ";
        $sql .= "             `exchange_id`, ";
        $sql .= "             `calls_in`) ";
        $sql .= "VALUES      ('" . date('Y-m-d H:i:s', $time) . "', ";
        $sql .= "             '$exchange_id', ";
        $sql .= "             '1') ";
        $sql .= "ON DUPLICATE KEY UPDATE ";
        $sql .= "             `calls_in` = `calls_in`+1;";

// add call stats to outgoing nodes
        $sql .= "INSERT INTO `" . node_name_formatter($o_route) . "` ";
        $sql .= "            (`timeperiod`, ";
        $sql .= "             `exchange_id`, ";
        $sql .= "             `calls_out`) ";
        $sql .= "VALUES      ('" . date('Y-m-d H:i:s', $time) . "', ";
        $sql .= "             '$exchange_id', ";
        $sql .= "             '1') ";
        $sql .= "ON DUPLICATE KEY UPDATE ";
        $sql .= "             `calls_out` = `calls_out`+1";

       // echo $sql . '<br><br>';
        mysqli_multi_query($connection, $sql) or die(mysqli_error($connection));

我正在按照php 手册中的说明进行此查询。使用这个函数还是按照正常的 mysqli_query 方式执行 2 个单独的查询更好?

4

1 回答 1

0

更新为

// add call stats to incoming nodes
        $sql = "INSERT INTO `" . node_name_formatter($i_route) . "` ";
        $sql .= "            (`timeperiod`, ";
        $sql .= "             `exchange_id`, ";
        $sql .= "             `calls_in`) ";
        $sql .= "VALUES      ('" . date('Y-m-d H:i:s', $time) . "', ";
        $sql .= "             '$exchange_id', ";
        $sql .= "             '1') ";
        $sql .= "ON DUPLICATE KEY UPDATE ";
        $sql .= "             `calls_in` = `calls_in`+1;";

// add call stats to outgoing nodes
        $sql .= "INSERT INTO `" . node_name_formatter($o_route) . "` ";
        $sql .= "            (`timeperiod`, ";
        $sql .= "             `exchange_id`, ";
        $sql .= "             `calls_out`) ";
        $sql .= "VALUES      ('" . date('Y-m-d H:i:s', $time) . "', ";
        $sql .= "             '$exchange_id', ";
        $sql .= "             '1') ";
        $sql .= "ON DUPLICATE KEY UPDATE ";
        $sql .= "             `calls_out` = `calls_out`+1;";

       // echo $sql . '<br><br>';
        mysqli_multi_query($connection, $sql) or die(mysqli_error($connection));

您必须使用 mysqli_next_result 遍历结果以获取每个查询的结果。

在此处查看有关错误命令不同步的更多信息

于 2013-11-01T10:40:51.757 回答