PHP mysqli: Commands out of sync, you can't run this command now
Commands out of sync; you can't run this command nowShort answer
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. Free or fully fetch the earlier result before the next query. Stored procedure calls and multi_query return several results, and every one must be consumed.
Fix it fast
1Find out which cause you have
Find the query that ran just before the failing one, and ask whether its result was fully read or freed. List every place that sends a query.
grep -rn 'query(\|execute(\|multi_query(' src/2Match what you see, then jump to its fix
- 1
The error appears on the second query in a row, on the same connection.The previous result was never freed or fetchedFix - 2
The error follows a CALL statement.A stored procedure call returned more than one resultFix - 3
The error follows multi_query with several statements separated by semicolons.multi_query results were not all consumedFix
- 1
What does the error look like?
Commands out of sync; you can't run this command now
Cause 1 of 3
The previous result was never freed or fetched
1Check
The MySQL manual gives the rule: a new query before the earlier result is freed, or two result-returning queries without a store or use call between them, gives this error. Look for a query whose result you only partly read.
grep -n "query(\|prepare(\|fetch\|free" src/Repository.php2Fix
Buffer the result with store_result and free it before the next statement. With prepared statements, store or free the result first.
$stmt = $mysqli->prepare('SELECT id, name FROM users WHERE id = ?');
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->store_result();
// ... bind_result / fetch ...
$stmt->free_result();
$stmt->close();3Verify
Run the two queries back to back; the error is gone.
php script.phpConfirmed by dev.mysql.com
Cause 2 of 3
A stored procedure call returned more than one result
1Check
The PHP manual says a stored procedure can return extra, hidden result sets, that mysqli::query cannot fetch them correctly, and that failing to fetch all of them causes an error.
grep -rn "CALL " src/2Fix
Run CALL with multi_query (or real_query) and walk every result with next_result until none is left.
$mysqli->multi_query('CALL p()');
do {
if ($result = $mysqli->store_result()) {
$rows = $result->fetch_all(MYSQLI_ASSOC);
$result->free();
}
} while ($mysqli->next_result());3Verify
A query after the CALL runs without the error.
php script.phpConfirmed by php.net
Cause 3 of 3
multi_query results were not all consumed
1Check
The manual states that all result sets returned by a multi query must be fetched. A loop that stops after the first result leaves the rest pending.
grep -rn "multi_query\|next_result" src/2Fix
Keep the do/while next_result loop to the end, and check the error code when it stops, because next_result also stops on a failed statement.
$mysqli->multi_query($sql);
do {
if ($result = $mysqli->store_result()) {
$result->free();
}
} while ($mysqli->next_result());
if ($mysqli->errno) {
throw new RuntimeException($mysqli->error);
}3Verify
Run another query on the same connection afterwards.
php script.phpConfirmed by php.net
How do I stop it coming back?
- Wrap every query in a small function that always frees the result before returning.
- Do not share one connection between code that starts a query and code that reads it later.
- Use the same fetch pattern everywhere; mixing patterns hides leftover results.
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
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 fixBack end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fix