`
Lamp兄弟
  • 浏览: 16991 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now

阅读更多

 

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
 

 

$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");
$mysqli->multi_query("set names 'utf8'");
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();
?> 
 

 

分享到:
评论

相关推荐

    leetcode答案-goStudy:去学习

    原来是必须取得result,并执行result.Close()。原理是什么? 2020.10.26 照着的套路,用Go+Gin 把后台的部分重写了一遍,搞了两天才能正确跑起来。这个部分移出到独立的repo 继续做。 go get 扫雷: 中国大陆用go get...

    Python django使用多进程连接mysql错误的解决方法

    问题 mysql 查询出现错误 ... you can’t run this command now in your client code, you are calling client functions in the wrong order.  This can happen, for example, if you are using mysql

    MysqlError Commands out of sync; you can&#039;t run this command now SHOW TABLES解决方案.docx

    。。。

    django 连接数据库出现1045错误的解决方式

    根据菜鸟教程Django教程学习,运行”python manage.py migrate” 报错,出现 django.db.utils.OperationalError: (1045, “Access denied for user ‘账号’@’localhost’ (using password: YES)”) 错误。 这种...

    MySQL 一次执行多条语句的实现及常见问题

    MySQL是支持在单个查询字符串中指定多语句执行的,使用方法是给链接指定参数: 代码如下: //链接时设定 mysql_real_connect( …, CLIENT_MULTI_STATEMENTS ... you can’t run this command now 官方推荐的执行语句是这

    mysql-connector-net-8.0.21.msi

    MySqlCommand:执行一条sql语句。 MySqlDataReader: 包含sql语句执行的结果,并提供一个方法从结果中阅读一行。 MySqlTransaction: 代表一个SQL事务在一个MySQL数据库。 MySqlException: MySQL报错时返回的Exception...

    TPM-Rev-2.0-Part-3-Commands-01.38.pdf

    Trusted Platform Module Library Part 3: Commands This TPM 2.0 Part 3 of the Trusted Platform Module Library specification contains the definitions of the TPM commands. These commands make use of the ...

    Sybase Reference Manual:commands

    Sybase数据库命令集参考手册: Sybase Reference Manual:commands.pdf

    PHP and MySQL Web Devepopment 4th Edition.pdf

    By simply entering the DCDIAG command does get the job done, but this would not be much of an article if I just told you to run the command, and left it at that. There is a lot more to the Domain ...

    pycharm中导入模块错误时提示Try to run this command from the system terminal

    pycharm中导入模块错误时,提示:Try to run this command from the system terminal. Make sure that you use the correct version of ‘pip’ installed for your Python interpreter located atpycharm工作路径。...

    mysql存储过程之case语句用法实例详解

    本文实例讲述了mysql存储过程之case语句用法。分享给大家供大家参考,具体如下: 除了if语句,mysql提供了一个替代的条件语句CASE。 mysql的 CASE语句使代码更加可读和高效。废话不多说,我们先来看下简单case语句的...

    Advanced Apple Debugging & Reverse Engineering v0.9.5

    The LLDB command command regex acts much like command alias, except you can provide a regular expression for input which will be parsed and applied to the action part of the command. 10. Assembly ...

    lucene lsql

    It can be run in interactive mode, or can automatically parse a list of commands from a file. Example LSql queries are: SELECT field1,field2 WHERE +field1:value This command will display two ...

    CE中文版-启点CE过NP中文.exe

    November 13 2017:Can't run Cheat Engine There is apparently some malware going around that blocks execution of Cheat Engine (Saying file missing, check filename, etc...) If you have been a victim of ...

    ANSYS Mechanical APDL Command Reference.pdf

    Welcome to the Command Reference.This reference contains a complete dictionary of detailed command descriptions, arranged in alphabetical order. It is the definitive resource for correct command ...

    linux bash手册(英文,version 4.2)

    Shell Builtin Commands:: Commands that are a part of the shell. Shell Variables:: Variables used or set by Bash. Bash Features:: Features found only in Bash. Job Control:: What job control is and how...

    Windows Server 2008 Portable Command Guide_ MCTS 70-640, 70-642, 70-643

    I started the outline of this book by ensuring that command prompt commands covered by the Microsoft Certified Information Technology Professional (MCITP) certifications on Windows Server 2008 were...

    C 语言编缉神经网络工具

    run even if you don't have a co-processor. If you have a coprocessor and want batchnet to run faster, which may be especially important in training, you can recompile batchnet.c using the 80x87 ...

    UE(官方下载)

    The benefit of a column maker is that it can help you to format your text/code, or in some cases to make it easier to read in complex nested logic. Quick Open UltraEdit and UEStudio provide multiple ...

    Command Line Fundamentals

    By the end of this book, you’ll have explored the basics of shell scripting, allowing you to easily and quickly automate tasks. Contents What You Will Learn Use the Bash shell to run commands Utilize...

Global site tag (gtag.js) - Google Analytics