How to Use mysqli_stmt::store_result and mysqli_stmt::next_result Together?
When working with PHP’s MySQLi extension for database operations, special attention is required when dealing with multi-result sets. In such cases, the methods mysqli_stmt::store_result and mysqli_stmt::next_result often need to be used together to ensure data is read correctly and the connection state remains stable.
1. Background Knowledge
When executing a statement in MySQL that can return multiple result sets (such as calling a stored procedure or executing multiple concatenated SELECT statements), PHP’s MySQLi extension receives these result sets sequentially. If not handled properly, a "Commands out of sync" error may occur.
-
mysqli_stmt::store_result
Buffers the current result set from the MySQL server to the client, allowing safe reading and iteration of data afterward. -
mysqli_stmt::next_result
Moves to the next result set. If multiple result sets remain unprocessed and the connection is closed directly, errors often occur.
2. Use Cases
-
Calling stored procedures that return multiple SELECT results.
-
Executing multiple SQL statements at once and iterating over results individually.
-
Ensuring all result sets are fully processed to avoid connection state issues.
3. Example Code
<?php
</span></span><span><span class="hljs-subst">$mysqli</span></span><span> = new mysqli("localhost", "user", "password", "testdb");
if (</span><span><span class="hljs-subst">$mysqli</span></span><span>->connect_errno) {
die("Connection failed: " . </span><span><span class="hljs-subst">$mysqli</span></span><span>->connect_error);
}
// Assume test_procedure returns two result sets
</span><span><span class="hljs-subst">$stmt</span></span><span> = </span><span><span class="hljs-subst">$mysqli</span></span><span>->prepare("CALL test_procedure()");
</span><span><span class="hljs-subst">$stmt</span></span><span>->execute();
// Loop through multiple result sets
do {
// Buffer the current result set to the client
if (</span><span><span class="hljs-subst">$stmt</span></span><span>->store_result()) {
while (</span><span><span class="hljs-subst">$row</span></span><span> = </span><span><span class="hljs-subst">$stmt</span></span><span>->fetch()) {
// Process data here
var_dump(</span><span><span class="hljs-subst">$row</span></span><span>);
}
}
// Move to the next result set
} while (</span><span><span class="hljs-subst">$stmt</span></span><span>->next_result());
</span><span><span class="hljs-subst">$stmt</span></span><span>->close();
</span><span><span class="hljs-subst">$mysqli</span></span><span>->close();
</span></span>
4. Key Points
-
Even if you do not need to process a particular result set, call it to ensure data is properly released. -
Use with next_result
After reading a result set, you must call next_result to move to the next one; otherwise, subsequent operations may be blocked. -
Error Handling
In complex stored procedures, it is recommended to include error checks within the loop so that any issues with the result set can stop the process promptly.
5. Conclusion
When dealing with MySQL multi-result sets, mysqli_stmt::store_result and mysqli_stmt::next_result are essential tools. The former safely caches result sets, while the latter iterates to the next set. Using them together ensures PHP applications handle multi-result scenarios reliably and correctly, avoiding common connection errors.
ARTICLE;
<span></span>
Related Tags:
mysqli_stmt