• PHP Video Tutorials

PHP mysqli_use_result() Function



Definition and Usage

The mysqli_use_result() function starts the retrieval of the resultset from the previously executed query

Syntax

mysqli_use_result($con)

Parameters

Sr.No Parameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.

Return Values

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

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_use_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->use_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_use_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
php_function_reference.htm
Advertisements