PHP mysqli_next_result() Function
Definition and Usage
The mysqli_next_result() function prepares the next result from the previous multi-query. You can retrieve the prepared resultset using the mysqli_use_result() function.
Syntax
mysqli_next_result($con)
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
con(Mandatory) This is an object representing a connection to MySQL Server. |
Return Values
The mysqli_next_result() function returns true if there are more resultsets and it returns false if there are no more result sets or, if the next query have errors.
PHP Version
This function was first introduced in PHP Version 5 and works works in all the later versions.
Example
Following example demonstrates the usage of the mysqli_next_result() function (in procedural style) −
<?php
//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "test");
//Executing the multi query
$query = "SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials";
$res = mysqli_multi_query($con, $query);
$count = 0;
if ($res) {
do {
$count = $count+1;
mysqli_use_result($con);
} while (mysqli_next_result($con));
}
print("Number of result sets: ".$count);
mysqli_close($con);
?>
This will produce following result −
Number of result sets: 3
Example
In object oriented style the syntax of this function is $con->next_result(); Following is the example of this function in object oriented style $minus;
<?php
$con = new mysqli("localhost", "root", "password", "test");
//Multi query
$res = $con->multi_query("SELECT * FROM players;SELECT * FROM emp;SELECT * FROM tutorials");
$count = 0;
if ($res) {
do {
$count = $count+1;
$con-> use_result();
} while ($con->next_result());
}
print("Number of result sets: ".$count);
//Closing the connection
$res = $con -> close();
?>
This will produce following result −
Number of result sets: 3
Example
Following example retrieves the records of all the resultsets of the muti-query −
//Creating a connection
$con = mysqli_connect("localhost", "root", "password", "test");
//Executing the multi query
$query = "SELECT * FROM players;SELECT * FROM emp";
$res = mysqli_multi_query($con, $query);
if ($res) {
do {
if ($result = mysqli_use_result($con)) {
while ($row = mysqli_fetch_row($result)) {
print("Name: ".$row[0]."\n");
print("Age: ".$row[1]."\n");
}
mysqli_free_result($result);
}
if (mysqli_more_results($con)) {
print("\n");
}
} while (mysqli_next_result($con));
}
mysqli_close($con);
This will produce following result −
Name: Dhavan Age: 33 Name: Rohit Age: 28 Name: Kohli Age: 25 Name: Raju Age: 25 Name: Rahman Age: 30 Name: Ramani Age: 22