How can a PHP developer handle multiple result sets when querying a database?
When querying a database in PHP, a developer can handle multiple result sets by using the `mysqli_multi_query()` function to execute multiple queries in a single call. This allows the developer to retrieve multiple result sets from the database and process them accordingly.
// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Query to retrieve multiple result sets
$query = "
SELECT * FROM table1;
SELECT * FROM table2;
";
// Execute the multi-query
if (mysqli_multi_query($connection, $query)) {
do {
if ($result = mysqli_store_result($connection)) {
while ($row = mysqli_fetch_assoc($result)) {
// Process the result set
}
mysqli_free_result($result);
}
} while (mysqli_next_result($connection));
}
// Close the database connection
mysqli_close($connection);
Keywords
Related Questions
- Are there any best practices for optimizing PHP code to handle large data insertions into a database more efficiently?
- What are the potential pitfalls of using include() as a function with a return value in PHP, and how can they be avoided?
- What are some alternative methods for dynamically changing links in a CMS based on the type of layout being used?