In PHP, next_result() is a function used to process multiple query results in MySQL. It belongs to a method in mysqli extension. This method is mainly used when you execute multiple SQL queries, which can easily skip the current result set and continue to process the next query result set. It is very useful when executing multiple SELECT queries or stored procedures.
The next_result() function allows you to process the result set of each query in turn after calling mysqli_query() or mysqli_multi_query() to execute multiple SQL queries. Each time next_result() is called, it will jump to the result of the next query until all results are processed.
bool mysqli_next_result(mysqli $link);
link : mysqli connection object, indicating the database connection you have established.
Returns true when successful, indicating that the switch to the next result set has been successfully switched to the next result set.
Returns false on failure, usually because there are no more result sets to handle.
next_result() is most commonly used in the following cases:
Execute multiple SELECT queries : When you execute multiple queries at once and each query has a result, you need to make sure that the results of each query are obtained in order.
Execute stored procedures : Multiple result sets may be returned in stored procedures. Using next_result() can help you process each result set one by one.
Here is an example showing how to use next_result() to handle multiple query results:
<?php
// Connect to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Multiple SQL Query
$sql = "SELECT * FROM users; SELECT * FROM orders;";
// 执行多个Query
if ($mysqli->multi_query($sql)) {
// 处理第一个Query的结果集
do {
if ($result = $mysqli->store_result()) {
while ($row = $result->fetch_assoc()) {
echo "UserID: " . $row['id'] . " - User名: " . $row['name'] . "<br>";
}
$result->free();
}
// 继续到下一个Query结果集
} while ($mysqli->next_result());
} else {
echo "Query错误: " . $mysqli->error;
}
// Close the connection
$mysqli->close();
?>
Connect to the database : Use the new mysqli() method to connect to the database.
Execute multiple queries : Execute statements containing multiple SQL queries through multi_query() . In this example, we query the two tables of users and orders .
Process query results : Use store_result() to get the result set of the current query, and output the data line by line through fetch_assoc() . After each result set is processed, call next_result() to jump to the result set of the next query.
Close connection : Use the close() method to close the database connection.
Why must next_result() be used in mysqli_multi_query() ?
Because mysqli_multi_query() supports execution of multiple queries at once, while next_result() is used to obtain the result set of each query one by one after multiple queries are executed. Without multi_query() , next_result() will not work correctly.
How to handle queries without result sets?
next_result() will continue to skip queries without results. For example, if your query statement is a UPDATE or DELETE statement and does not return the result set, next_result() will skip the query until the next query with a result.
How to use next_result() when executing stored procedures?
There may be multiple result sets in stored procedures, and next_result() can help get these results one by one. For example, you can execute multiple queries in a stored procedure and get the results of each query through next_result() .
next_result() is a powerful tool that helps developers efficiently handle multi-query result sets. It is especially useful when executing multiple SQL queries using mysqli_multi_query() , avoiding manual switching of result sets, simplifying code and increasing efficiency. When encountering stored procedures or multiple queries during development, understanding and using next_result() will greatly improve your development efficiency.
Hope this article helps you! If you have any other questions or need more help, feel free to ask.