PHP | mysqli_next_result() Function
January 24, 2021
Hello folks! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP mysqli_next_result() Function.
The built-in PHP mysqli_next_result() function prepares the next result from the previous multi-query. You can retrieve the prepared resultset using the built-in PHP mysqli_use_result() function.
The built-in PHP mysqli_next_result() function prepares the next result from the previous multi-query. You can retrieve the prepared resultset using the built-in PHP mysqli_use_result() function.
Syntax
Following below is the syntax to use this function -
mysqli_next_result($con)
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | con(Mandatory) This is an object representing a connection to MySQL Server. |
Return Value
This built-in PHP function returns true if there are more resultsets and false if there are no more resultsets, or if the next query have errors.
PHP Version
This built-in PHP function was first introduced in PHP version 5 and works in all the later versions.
Example1
The following below is an example which demonstrates the usage of PHP mysqli_next_result() function (in a 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); ?>
Output
When the above code is executed, it will produce the following result -
Number of result sets: 3
Example2
In object oriented style the syntax of this built-in PHP function is $con->next_result(); The following is the example of this function in an 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(); ?>
Output
When the above code is executed, it will produce the following result -
Number of result sets: 3
Example3
The following example below retrieves the records of all resultsets of the multi-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);
Output
When the above code is executed, it will produce the following result -
Name: Prince Age: 28 Name: Bethel Age: 27 Name: Stephanie Age: 24 Name: Kennedy Age: 27 Name: Paul Age: 30 Name: Justice Age: 28
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we will discuss about the PHP mysqli_options() Function.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.
Do feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.
Thanks for reading and bye for now.