zoukankan      html  css  js  c++  java
  • [轉]PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决

    原文地址: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 先给出代码:

    存储过程:

    CREATE PROCEDURE test1()
    begin
            drop table if exists tb1;
        create table tb1
         (
             val int not null
         )engine = innoDB;
        insert into tb1(val) values(1),(2),(3);
        select * from tb1;
    end

    PHP代码:

    <?php
    $mysqli = new mysqli("localhost", "root", "sbqcel", "test");

    if (mysqli_connect_errno())
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $result = null;
    $mysqli->autocommit(FALSE);
    if(!($result = $mysqli->query( "call test1();")))
    {
        echo mysqli_error($link);
        $mysqli->rollback();
    }
    $mysqli->commit();

    print 'Result1:';

    while ($row = $result->fetch_row())
    {
            printf ("%s <br />", $row[0]);
    }
    $result->close();
    mysqli_free_result($result);

    echo 'result2:<br />';
    if ($result2 = $mysqli->query("select val from tb1;"))
    {
        while ($row = $result2->fetch_row())
         {
            printf ("%s <br />", $row[0]);
         }
        $result2->close();
    }
    else
    {
        echo $mysqli->error;
    }
    mysqli_free_result($result2);

    mysqli_close($link);
    ?>

    执行上面的代码后就会出现上面的错误,消息说明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
    $mysqli = new mysqli("localhost", "root", "sbqcel", "test");

    if (mysqli_connect_errno())
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    echo 'result1:<br />';
    $mysqli->autocommit(FALSE);
    if ($mysqli->multi_query("call test1();"))
    {
        do {
            if ($result = $mysqli->store_result()) {
                while ($row = $result->fetch_row()) {
                    printf("%s\n", $row[0]);
                 }
                $result->close();
             }
         } while ($mysqli->next_result());
    }
    $mysqli->commit();
    echo "<br />";
    echo "result2:<br />";
    if ($result2 = $mysqli->query("select val from tb1;"))
    {
        while ($row = $result2->fetch_row()) {
            printf ("%s <br />", $row[0]);
         }
        $result2->close();
    }
    else
    {
        echo $mysqli->error;
    }
    $mysqli->close();
    ?>

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

    存储过程:

    CREATE PROCEDURE test1()
    begin
            drop table if exists tb1;
        create table tb1
         (
             val int not null
         )engine = innoDB;
        insert into tb1(val) values(1),(2),(3);
        select * from tb1;
    end

    PHP代码:

    Code1
    <?php
    $mysqli = new mysqli("localhost", "root", "sbqcel", "test");

    if (mysqli_connect_errno())
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $result = null;
    $mysqli->autocommit(FALSE);
    if(!($result = $mysqli->query( "call test1();")))
    {
        echo mysqli_error($link);
        $mysqli->rollback();
    }
    $mysqli->commit();

    print 'Result1:';

    while ($row = $result->fetch_row())
    {
            printf ("%s <br />", $row[0]);
    }
    $result->close();
    mysqli_free_result($result);

    echo 'result2:<br />';
    if ($result2 = $mysqli->query("select val from tb1;"))
    {
        while ($row = $result2->fetch_row())
         {
            printf ("%s <br />", $row[0]);
         }
        $result2->close();
    }
    else
    {
        echo $mysqli->error;
    }
    mysqli_free_result($result2);

    mysqli_close($link);
    ?>

    执行上面的代码后就会出现上面的错误,消息说明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
    $mysqli = new mysqli("localhost", "root", "sbqcel", "test");

    if (mysqli_connect_errno())
    {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    echo 'result1:<br />';
    $mysqli->autocommit(FALSE);
    if ($mysqli->multi_query("call test1();"))
    {
        do {
            if ($result = $mysqli->store_result()) {
                while ($row = $result->fetch_row()) {
                    printf("%s\n", $row[0]);
                 }
                $result->close();
             }
         } while ($mysqli->next_result());
    }
    $mysqli->commit();
    echo "<br />";
    echo "result2:<br />";
    if ($result2 = $mysqli->query("select val from tb1;"))
    {
        while ($row = $result2->fetch_row()) {
            printf ("%s <br />", $row[0]);
         }
        $result2->close();
    }
    else
    {
        echo $mysqli->error;
    }
    $mysqli->close();
    ?>

  • 相关阅读:
    win10 点击开始按钮无反应
    Server Error in '/' Application. IIS拒绝访问
    vs2017添加引用时报错未能正确加载“ReferenceManagerPackage”包。
    关于JavaScript中的this指向问题
    4p-在一张图片中根据矩形四个点的坐标计算两个矩形是否相交
    opencv使用
    [VMware]虚拟网络编辑器
    [所思所想]观《长津湖》有感
    [项目管理]失败的软件项目所具备的特点【待续】
    [软件过程/软件生命周期模型]软件过程的工具链【待续】
  • 原文地址:https://www.cnblogs.com/Athrun/p/2092896.html
Copyright © 2011-2022 走看看