PDO refuses to run a query because other unbuffered queries are still active
SQLSTATE[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.
Fix it fast
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 .2Match what you see, then jump to its fix
- 1
Cannot 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
The 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
Cannot 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
- 1
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
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 602Fix
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.phpConfirmed by php.net
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 402Fix
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Back end
PHP mysqli: Commands out of sync, you can't run this command now
Commands out of sync; you can't run this command now
The MySQL client library was called in the wrong order: a new query ran while the result of the previous one was still waiting to be read.
3 causes, each with a fixBack 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 fixBack end
CodeIgniter 4: try/catch does not catch a failed insert in the model
DatabaseException
CodeIgniter throws a DatabaseException on a failed query only when DBDebug is true, and inside a transaction it does not throw unless you ask.
3 causes, each with a fixBack end
CodeIgniter 4 where() builds a different condition than you meant
A SQL syntax error that appears only when a value contains a quote character
The query builder joins every where() with AND and every orWhere() with OR, with no brackets.
3 causes, each with a fixBack end
Call to undefined function mysql_pconnect() after a PHP upgrade
Call to undefined function mysql_connect()
The old mysql_* functions belonged to the ext/mysql extension, which PHP removed in version 7.0.
2 causes, each with a fixBack end
CodeIgniter cannot connect to the MySQL database server
Unable to connect to your database server using the provided settings.
CodeIgniter only reports that its connection failed.
4 causes, each with a fix