Maaz Zindani

Loading the index…

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

PHP mysqli: Commands out of sync, you can't run this command now

ErrorCommands out of sync; you can't run this command now

Short 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.

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

Fix it fast

  1. 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/
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on the second query in a row, on the same connection.The previous result was never freed or fetchedFix
    2. 2The error follows a CALL statement.A stored procedure call returned more than one resultFix
    3. 3The error follows multi_query with several statements separated by semicolons.multi_query results were not all consumedFix

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.php

2Fix

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.php

Confirmed by dev.mysql.com

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

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