Home >Database >Mysql Tutorial >Why Am I Getting a 'Commands Out of Sync' Error When Running Multiple MySQL Queries in PHP?
"Command out of sync" error in PHP
When executing multiple MySQL queries using mysqli, you may encounter the "Command out of sync; you cannot run this command now" error. This error occurs because mysqli uses non-buffered queries by default, which means that the results of the first query must be retrieved before subsequent queries are executed.
Error reason
In the provided PHP code, two MySQL queries are being executed:
$data = $con->query($countQuery)
$rows = getRowsByArticleSearch("test", "Auctions", " ")
This error is most likely triggered when a second query is executed before the results of the first query are retrieved.
Solution
There are two ways to solve this problem:
1. Get the results of the first query
Use the mysqli_fetch_*()
method to retrieve the results of the first query before executing the second query. For example:
<code class="language-php">$data = $con->query($countQuery); $rowcount = $data->num_rows;</code>
2. Buffered query
Use mysqli_stmt::store_result()
to enable query buffering. This will cause mysqli to store the results of the first query before executing the second query.
<code class="language-php">$numRecords->store_result();</code>
Other instructions
$countQuery
that there is a syntax error in the query. Make sure the query is built correctly. mysqli::multi_query()
method. This method handles buffering and synchronization automatically. The above is the detailed content of Why Am I Getting a 'Commands Out of Sync' Error When Running Multiple MySQL Queries in PHP?. For more information, please follow other related articles on the PHP Chinese website!