What is causing PDO error Cannot execute queries while other unbuffered queries are active?

前端 未结 4 637
野的像风
野的像风 2021-02-01 09:01

I have the following code:

$dbh = new PDO(\"mysql:host=$host;dbname=$dbname\", $user, $pass);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->         


        
相关标签:
4条回答
  • 2021-02-01 09:20

    This is not necessarily the answer to this question, but this may help somebody in the future.

    I came across exactly the same error and it took hours to discover what was wrong. It turned out it was just a extremely minor syntax issue all along. If you're not actually using any buffering, but still have this error, like I did, this could be your issue - so check your code.

    I was doing my normal database queries when I came across this error -- not purposely using any buffering techniques -- so I highly doubted it had anything to do with buffering. I read every SO question about it and looked deeper in to it.

    This was my STUPID syntax issue:

    $SQL = "UPDATE articles SET
                topicID = :topic;    <-------- semicolon - woops!
                heading = :heading,
                subheading = :subheading,
                keywords = :keywords,
                rawContent = :rawContent,
                content = :content,
                ...
                ...
    

    This resulted in me getting this buffering error. I fixed the code and it went away. What was most annoying, was the fact the PDO error was pointing at another query, the next query, but that query was in a function elsewhere in the code, and that through me well off course for a while!

    0 讨论(0)
  • 2021-02-01 09:20

    Just to complete the list of possible mistakes causing this issue ... because I was loosing my hair on this I want to share my solution/finding with you.

    In my case I've tried to sent several statements to the database with PDO::exec

    e.g.

    self::$objDatabase->exec( "SELECT id from testtable; UPDATE testtable SET name = 'example';" );
    

    Only 1 SQL Statement in 1 PDO::exec is allowed and save.

    0 讨论(0)
  • 2021-02-01 09:22

    It seems that you have PDO::MYSQL_ATTR_USE_BUFFERED_QUERY set to FALSE.

    And in such a case it is obligatory to make sure that there are no more rows pending for the retrieval. To do so one to run fetch() one extra time, as it seems that fetch() returning false is "releasing" non-buffered resultset somehow. Without such extra call non-buffered resultset remains locked and causing "Commands out of sync" error

    0 讨论(0)
  • 2021-02-01 09:26

    Oddly enough, the PHP packages provided by Ubuntu are not compiled with the Mysql native driver, but with the old libmysqlclient instead (tested on Ubuntu 13.10 with default packages):

    <?php
    echo $dbh->getAttribute(PDO::ATTR_CLIENT_VERSION); // prints "5.5.35", i.e MySQL version
    // prints "mysqlnd (...)" when using mysqlnd
    

    Your very test case ("Edit 4", with setAttribute(MYSQL_ATTR_USE_BUFFERED_QUERY, true)) works as expected with PHP 5.5.3 manually compiled with mysqlnd with:

    ./configure --with-pdo-mysql=mysqlnd # default driver since PHP v5.4
    

    ... but fails with:

    bash> ./configure --with-pdo-mysql=/usr/bin/mysql_config
    

    It quite odd that it fails only if the first statement is executed twice; this must be a bug in the libmysqlclient driver.

    Both drivers fail as expected when MYSQL_ATTR_USE_BUFFERED_QUERY is false. Your Common Sense already demonstrated why this is expected behaviour, regardless of the number of rows in the result set.

    Mike found out that the current workaround is installing the php5-mysqlnd package instead of the Canonical-recommended php5-mysql.

    0 讨论(0)
提交回复
热议问题