MySQLi Tutorial MySQLi References

mysqli store_result() Method



The mysqli::store_result() / mysqli_store_result() function is used to transfer the result set from the last query on the database connection represented by the mysql parameter.

Syntax

//Object-oriented style
public mysqli::store_result(mode)

//Procedural style
mysqli_store_result(mysql, mode)

Parameters

mysql Required. For procedural style only: Specify a mysqli object returned by mysqli_connect() or mysqli_init().
mode Optional. Specify option. It can be one of the following:
  • MYSQLI_STORE_RESULT_COPY_DATA - Copy results from the internal mysqlnd buffer into the PHP variables fetched. By default, mysqlnd will use a reference logic to avoid copying and duplicating results held in memory. For certain result sets, for example - result sets with many small rows, the copy approach can reduce the overall memory usage because PHP variables holding results may be released earlier (available with mysqlnd only).

Return Value

Returns a buffered result object or false if an error occurred.

Note: This function returns false in case the query didn't return a result set or if the reading of the result set failed. Also possible reason for this function returning false after successful call to mysqli_query() can be too large result set.

Example: Object-oriented style

The example below shows the usage of mysqli::store_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 result set in PHP
  if ($result = $mysqli->store_result()) {
    while ($row = $result->fetch_row()) {
      printf("%s\n", $row[0]);
    }
  }

  //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_store_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 result set in PHP
  if ($result = mysqli_store_result($mysqli)) {
    while ($row = mysqli_fetch_row($result)) {
      printf("%s\n", $row[0]);
    }
  }
  
  //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

❮ MySQLi Functions Reference