English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Examples of PHP mysqli_next_result() Function

PHP MySQLi Reference Manual

The mysqli_next_result() function prepares the next result set for mysqli_multi_query().

Definition and Usage

After mysqli_multi_query() function is executed, prepare to read the next result set, and then use mysqli_store_result() or mysqli_use_result() function to read the next result set.

Syntax

mysqli_next_result($con)

Parameter

Serial NumberParameters and Description
1

con(Necessary)

This is an object representing the connection with the MySQL Server.

Return value

If there are more result sets, the mysqli_next_result() function will return true; if there are no more result sets, or if the next query has an error, it will return false.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Online Example

The following examples demonstratemysqli_next_result()Usage of the function (procedural style)-

<?php
   //Establish connection
   $con  =  mysqli_connect("localhost",  "root",  "password",  "test");
   //Execute multiple queries
   $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 Result

Number of result sets: 3

Online Example

In the object-oriented style, the syntax of this function is:}}$con-> next_result();.The following is an example of object-oriented style functions;

<?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);
   //Close connection
   $res  =  $con ->  close();
?>

Output Result

Number of result sets: 3

Online Example

The following example retrieves all result sets of multiple queries-

//Establish connection
$con  =  mysqli_connect("localhost",  "root",  "password",  "test");
//Execute multiple queries
$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 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

PHP MySQLi Reference Manual