MySQLi Tutorial MySQLi References

mysqli_stmt store_result() Method



The mysqli_stmt::store_result() / mysqli_stmt_store_result() function is used to store a result set in an internal buffer.

This function should be called for queries that successfully produce a result set (e.g. SELECT, SHOW, DESCRIBE, EXPLAIN) only if the complete result set needs to be buffered in PHP. Each subsequent mysqli_stmt_fetch() call will return buffered data.

Note: It is unnecessary to call this function for queries where result set is not produced. But if called it will not cause any notable performance loss. To check whether a query produces a result set, the mysqli_stmt_result_metadata() function can be used.

Syntax

//Object-oriented style
public mysqli_stmt::store_result()

//Procedural style
mysqli_stmt_store_result(statement)

Parameters

statement Required. For procedural style only: Specify a mysqli_stmt object returned by mysqli_stmt_init().

Return Value

Returns true on success or false on failure.

Example: Object-oriented style

The example below shows the usage of mysqli_stmt::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();
}

//preparing an SQL statement for execution
$query = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$stmt = $mysqli->prepare($query);

//executing the SQL statement
$stmt->execute();

//storing the result in an internal buffer
$stmt->store_result();

//getting the number of rows buffered in statement
printf("Number of rows: %d \n", $stmt->num_rows);
?>

The output of the above code will be similar to:

Number of rows: 58

Example: Procedural style

The example below shows the usage of mysqli_stmt_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();
}

//preparing an SQL statement for execution
$query = "SELECT EmpID, Name, Age FROM Employee ORDER BY Age";
$stmt = mysqli_prepare($mysqli, $query);

//executing the SQL statement
mysqli_stmt_execute($stmt);

//storing the result in an internal buffer
mysqli_stmt_store_result($stmt);

//getting the number of rows buffered in statement
printf("Number of rows: %d \n", mysqli_stmt_num_rows($stmt));
?>

The output of the above code will be similar to:

Number of rows: 58

❮ MySQLi Functions Reference