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

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_use_result() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_use_result() function initializes the retrieval of the result set from the query executed last time using mysqli_real_query().

Definition and usage

mysqli_use_result()The function starts to retrieve the result set from the previous executed query.

Syntax

mysqli_use_result($con)

Parameter

Serial numberParameters and descriptions
1

con(required)

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

Return value

mysqli_use_result()The function returns the result object and the boolean value false in case of an error.

PHP version

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

Online Example

The following examples demonstratemysqli_use_result()Function usage (procedural style)-

<?php
   //Establish connection
   $con  =  mysqli_connect("localhost",  "root",  "password",  "test");
   //Execute multiple queries, separated by semicolons.
   $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 object-oriented style, the syntax of this function is }}$con->use_result();.Here is an example of this function in object-oriented style;

<?php
   $con  =  new  mysqli("localhost",  "root",  "password",  "test");
   //Multiple queries
   $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_use_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