Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PDO refuses to run a query because other unbuffered queries are still active

ErrorSQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.

Short answer

In unbuffered mode MySQL sends rows only as you fetch them, so the connection stays busy until the last row is read, and any new query on it fails with error 2014. PDO buffers by default, so look for code that turned buffering off, a query inside a fetch loop, or a statement left half-read. Fetch everything, call closeCursor(), or buffer again.

  • Applies to PHP 8.3, PHP 8.4, MySQL 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    The message has two forms, and they point at different causes: 'unbuffered queries are active' means rows of an unbuffered result are still unread, 'pending result sets' means more results are waiting on the connection. Then find out whether your code turns buffering off anywhere.

    grep -rn "USE_BUFFERED_QUERY" --include=*.php .
  2. 2Match what you see, then jump to its fix

    1. 1Cannot execute queries while other unbuffered queries are active, and the code sets USE_BUFFERED_QUERY to falseBuffering was turned off and the earlier result was not read to the endFix
    2. 2The error appears on the first pass through a while ($row = $stmt->fetch()) loop that calls $pdo->query() or another execute() insideA second query runs inside the loop that reads an unbuffered resultFix
    3. 3Cannot execute queries while there are pending result sets, after a statement of which only the first row was fetchedAn earlier statement was read only in part and still holds the connectionFix

What does the error look like?

  • SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.
  • Consider using PDOStatement::fetchAll().
  • Cannot execute queries while there are pending result sets. Consider unsetting the previous PDOStatement or calling PDOStatement::closeCursor()

Cause 1 of 3

Buffering was turned off and the earlier result was not read to the end

1Check

pdo_mysql runs statements in buffered mode by default. Code that sets the attribute to false gets unbuffered results, and then no further query can go over the same connection until the whole result set has been fetched.

grep -rn "MYSQL_ATTR_USE_BUFFERED_QUERY" --include=*.php .

2Fix

Remove the line or set it to true, so each result is copied to PHP at once. Keep unbuffered mode only for one very large result that you read row by row, and fetch it to the end before the next statement. On PHP 8.5 the constant is Pdo\Mysql::ATTR_USE_BUFFERED_QUERY; the PDO::MYSQL_ constants are deprecated there.

// buffered again (the default)
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);

// or read the result completely before the next statement
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

3Verify

Run the code path that failed. The second query now executes, and the result of the first is still in $rows.

<?php
var_dump(count($rows));

Confirmed by php.net

Did this fix it?

Cause 2 of 3

A second query runs inside the loop that reads an unbuffered result

1Check

In unbuffered mode the connection stays busy until the last row is fetched. Buffered mode, in contrast, allows further queries on the same connection while you work on the result set. Look for a nested query inside a fetch loop.

grep -rn -B2 -A6 "while *(.*->fetch" --include=*.php . | head -n 60

2Fix

Fetch the first result completely and loop over the array, or switch buffering on. For a result too big for memory, open a second PDO connection for the inner queries.

$outer = $pdo->query('SELECT id FROM orders');
foreach ($outer->fetchAll(PDO::FETCH_COLUMN) as $id) {
    $items = $pdo->prepare('SELECT * FROM order_items WHERE order_id = ?');
    $items->execute([$id]);
}

3Verify

The loop finishes without the 2014 error.

php your-script.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

An earlier statement was read only in part and still holds the connection

1Check

A statement can leave unfetched rows or further result sets on the connection, and the next query then fails. The manual says closeCursor() exists for drivers that cannot run a statement while an earlier one still has unfetched rows. Look for fetch() calls that read only the first row.

grep -rn -E "\->fetch\(" --include=*.php . | head -n 40

2Fix

Call closeCursor() on the statement once you have what you need. It frees the connection so other statements can run, and the statement can be executed again. Setting the statement variable to null also releases it.

$stmt = $pdo->prepare('SELECT foo FROM bar');
$stmt->execute();
$first = $stmt->fetch();
$stmt->closeCursor();

$next = $pdo->query('SELECT 1');

3Verify

The query after closeCursor() runs without an exception.

<?php
var_dump($next->fetchColumn());

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Leave PDO in its default buffered mode; the manual calls buffered queries the easier and more flexible way to process results.
  • Use unbuffered mode only for one big result that you read row by row, and send no other query on that connection until it is finished.
  • After reading only some rows of a result, call closeCursor() before the next statement.
  • Keep inner queries out of fetch loops; fetch the outer result into an array first.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

PDO throws SQLSTATE[HY093] Invalid parameter number

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens

HY093 means the values you give PDO do not line up with the placeholders in the SQL.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.