Jump to content

PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.


Go to solution Solved by DarioVanin,

Recommended Posts

Hi Guys,

I am stuck with an issue that prevents me from executing two queries in a row on my mysql database using PDO. The database works perfectly when I use mysqli, so the problem must be on my php.ini/code configuration.

This is the code:

function connect_pdo_db()
{
    global $pdodb;
    if (isset($pdodb) ) {
        return;
    }
    $host = 'myhost';
    $db   = 'mydbname';
    $user = 'myuser';
    $pass = 'mypassword';
    $charset = 'utf8mb4';

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
        PDO::ATTR_PERSISTENT         => true,
        PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false
    ];
    try {
         $pdodb = new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
         throw new \PDOException($e->getMessage(), (int)$e->getCode());
    }
}
global $pdodb;
connect_pdo_db();

$query = "CALL my_stored_procedure1(:user_login);";
$sth = $pdodb->prepare($query);
$sth->bindParam(':user_login', $user_login);
$sth->execute();
$output1 = $sth->fetchAll();

$query = "CALL my_stored_procedure2(:user_login);";
$sth = $pdodb->prepare($query);
$sth->bindParam(':user_login', $user_login);
$sth->execute();
$output2 = $sth->fetchAll();

If I run a single query without the last 5 lines of code, it works fine.

I tried wih the paramenter MYSQL_ATTR_USE_BUFFERED_QUERY both set to "true" and "false" with the same results. 

2022/12/03 09:21:40 [error] 32#32: *2 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

The extension on my php.ini is active

extension=pdo_mysql

Has any of you had the same problem? Can you please help me find a solution?

Thanks,

Dario Vanin

  • Solution

problem solved. I had to upgrade from php7 to php8 and add 

$sth->closeCursor();

after

$sth->fetchAll();

I also vhanged the optinos as below

      PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
      PDO::ATTR_EMULATE_PREPARES   => false,

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.