We now have a youtube channel. Subscribe!

PHP | mysqli_use_result() Function

PHP mysqli_use_result() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_use_result() Function.

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

Syntax

Following below is the syntax to use this function -

mysqli_use_result($con)


Parameter Details

Sr.NoParameter & Description
1

con(Mandatory)

This is an object representing a connection to MySQL Server.


Return Value

This built-in PHP function returns a result object on success and false in case of an error.

PHP Version

This 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 the built-in PHP mysqli_use_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 an object oriented style the syntax of this function is $con->use_result(); The following is the example of this function in an object oriented style -

<?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 below example retrieves the records of all the 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_use_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 going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the built-in mysqli_warning_count() Function in PHP.

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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain