PHP Function Reference

PHP mysqli use_result() Method



The PHP mysqli::use_result() / mysqli_use_result() function is used to initiate the retrieval of a result set from the last query executed using the mysqli_real_query() function on the database connection.

Either this or the mysqli_store_result() function must be called before the results of a query can be retrieved, and one or the other must be called to prevent the next query on that database connection from failing.

Note: This function does not transfer the entire result set from the database and hence cannot be used functions such as mysqli_data_seek() to move to a particular row within the set. To use this functionality, the result set must be stored using mysqli_store_result(). The mysqli_use_result() should not be used if a lot of processing on the client side is performed, since this will tie up the server and prevent other threads from updating any tables from which the data is being fetched.

Syntax

//Object-oriented style
public mysqli::use_result()

//Procedural style
mysqli_use_result()

Parameters

No parameter is required.

Return Value

Returns an unbuffered result object or false if an error occurred.

Example: Object-oriented style

The example below shows the usage of mysqli::use_result() method.

<?php
//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
  echo "Failed to connect to MySQL: ". $mysqli->connect_error;
  exit();
}

//string containing multiple queries
$sql = "SELECT CURRENT_USER();";
$sql .= "SELECT Name FROM Employee";

//executing multiple queries
$mysqli->multi_query($sql);
do {
  //storing the first result set
  if ($result = $mysqli->use_result()) {
    while ($row = $result->fetch_row()) {
      printf("%s\n", $row[0]);
    }
    $result->close();
  }

  //if there are more result-sets, printing divider
  if ($mysqli->more_results()) {
    printf("--------------\n");
  }
} while ($mysqli->next_result());

//closing the connection
$mysqli->close();
?>

The output of the above code will be similar to:

user@localhost
--------------
Marry
Kim
John
Adam

Example: Procedural style

The example below shows the usage of mysqli_use_result() function.

<?php
//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: ". mysqli_connect_error();
  exit();
}

//string containing multiple queries
$sql = "SELECT CURRENT_USER();";
$sql .= "SELECT Name FROM Employee";

//executing multiple queries
mysqli_multi_query($mysqli, $sql);
do {
  //storing the first result set
  if ($result = mysqli_use_result($mysqli)) {
    while ($row = mysqli_fetch_row($result)) {
      printf("%s\n", $row[0]);
    }
    mysqli_free_result($result);
  }
  
  //if there are more result-sets, printing divider
  if (mysqli_more_results($mysqli)) {
    printf("--------------\n");
  }
} while (mysqli_next_result($mysqli));

//closing the connection
mysqli_close($mysqli);
?>

The output of the above code will be similar to:

user@localhost
--------------
Marry
Kim
John
Adam

❮ PHP MySQLi Reference