原文地址:http://www.pczpg.com/a/2010/0507/7815.html

在PHP同一事物里调用MYSQL的存储过程后再次执行另外的一个或多个命令(或者在同一事物里执行多个存储过程),如果使用mysqli的query方法获得结果,将获得一个错误:Commands out of sync; you can't run this command now sss 先给出代码:

存储过程:

[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决CREATE PROCEDURE test1()
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决begin
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        drop table if exists tb1;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    create table tb1
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     (
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决         val int not null
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     )engine = innoDB;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    insert into tb1(val) values(1),(2),(3);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    select * from tb1;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决end

PHP代码:

[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决<?php
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli = new mysqli("localhost", "root", "sbqcel", "test");
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if (mysqli_connect_errno())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    printf("Connect failed: %s\n", mysqli_connect_error());
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    exit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$result = null;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->autocommit(FALSE);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if(!($result = $mysqli->query( "call test1();")))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    echo mysqli_error($link);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    $mysqli->rollback();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->commit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决print 'Result1:';
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决while ($row = $result->fetch_row())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        printf ("%s <br />", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$result->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决mysqli_free_result($result);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo 'result2:<br />';
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if ($result2 = $mysqli->query("select val from tb1;"))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    while ($row = $result2->fetch_row())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        printf ("%s <br />", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    $result2->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决else
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    echo $mysqli->error;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决mysqli_free_result($result2);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决mysqli_close($link);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决?>

执行上面的代码后就会出现上面的错误,消息说明MYSQL数据库认为是这一个错误的命令执行顺序。原因在于MYSQL的存储过程执行完成后除了返回实际结果集还会返回存储过程执行的转态

,而上面的代码仅处理了第一个结果集,第二个结果集并没有被释放掉。

When a stored procedure returns a resultset, MySQL returns at least two resultsets: first for the SELECT CALL inside the stored procedure. 2ndfor the call of the stored procedure itself

(2nd usually is only an OK or ERR packet).

要解决这个问题,需要用mysqli的multi_query方法,遍历所有的结果集并释放掉掉。代码如下:

[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决<?php
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli = new mysqli("localhost", "root", "sbqcel", "test");
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if (mysqli_connect_errno())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    printf("Connect failed: %s\n", mysqli_connect_error());
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    exit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo 'result1:<br />';
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->autocommit(FALSE);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if ($mysqli->multi_query("call test1();"))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    do {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        if ($result = $mysqli->store_result()) {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决            while ($row = $result->fetch_row()) {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决                printf("%s\n", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决             }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决            $result->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决         }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     } while ($mysqli->next_result());
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->commit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo "<br />";
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo "result2:<br />";
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if ($result2 = $mysqli->query("select val from tb1;"))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    while ($row = $result2->fetch_row()) {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        printf ("%s <br />", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    $result2->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决else
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    echo $mysqli->error;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决?>

在PHP同一事物里调用MYSQL的存储过程后再次执行另外的一个或多个命令(或者在同一事物里执行多个存储过程),如果使用mysqli的query方法获得结果,将获得一个错误:Commands out of sync; you can't run this command now sss 先给出代码:

存储过程:

[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决CREATE PROCEDURE test1()
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决begin
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        drop table if exists tb1;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    create table tb1
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     (
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决         val int not null
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     )engine = innoDB;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    insert into tb1(val) values(1),(2),(3);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    select * from tb1;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决end

PHP代码:

[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决Code1
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决<?php
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli = new mysqli("localhost", "root", "sbqcel", "test");
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if (mysqli_connect_errno())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    printf("Connect failed: %s\n", mysqli_connect_error());
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    exit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$result = null;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->autocommit(FALSE);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if(!($result = $mysqli->query( "call test1();")))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    echo mysqli_error($link);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    $mysqli->rollback();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->commit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决print 'Result1:';
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决while ($row = $result->fetch_row())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        printf ("%s <br />", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$result->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决mysqli_free_result($result);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo 'result2:<br />';
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if ($result2 = $mysqli->query("select val from tb1;"))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    while ($row = $result2->fetch_row())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        printf ("%s <br />", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    $result2->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决else
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    echo $mysqli->error;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决mysqli_free_result($result2);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决mysqli_close($link);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决?>

执行上面的代码后就会出现上面的错误,消息说明MYSQL数据库认为是这一个错误的命令执行顺序。原因在于MYSQL的存储过程执行完成后除了返回实际结果集还会返回存储过程执行的转态

,而上面的代码仅处理了第一个结果集,第二个结果集并没有被释放掉。

When a stored procedure returns a resultset, MySQL returns at least two resultsets: first for the SELECT CALL inside the stored procedure. 2ndfor the call of the stored procedure itself

(2nd usually is only an OK or ERR packet).

要解决这个问题,需要用mysqli的multi_query方法,遍历所有的结果集并释放掉掉。代码如下:

[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决<?php
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli = new mysqli("localhost", "root", "sbqcel", "test");
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if (mysqli_connect_errno())
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    printf("Connect failed: %s\n", mysqli_connect_error());
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    exit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo 'result1:<br />';
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->autocommit(FALSE);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if ($mysqli->multi_query("call test1();"))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    do {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        if ($result = $mysqli->store_result()) {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决            while ($row = $result->fetch_row()) {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决                printf("%s\n", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决             }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决            $result->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决         }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     } while ($mysqli->next_result());
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->commit();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo "<br />";
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决echo "result2:<br />";
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决if ($result2 = $mysqli->query("select val from tb1;"))
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    while ($row = $result2->fetch_row()) {
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决        printf ("%s <br />", $row[0]);
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决     }
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    $result2->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决else
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决{
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决    echo $mysqli->error;
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决}
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决$mysqli->close();
[轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决?>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
  • 2021-06-30
  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-06
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案