MySQLi Tutorial MySQLi References

mysqli_stmt free_result() Method



The mysqli_stmt::free_result() / mysqli_stmt_free_result() function is used to free the result memory associated with the statement, which was allocated by mysqli_stmt_store_result().

Syntax

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

//Procedural style
mysqli_stmt_free_result(statement)

Parameters

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

Return Value

No value is returned.

Example: Object-oriented style

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

//freeing the result set memory
$stmt->free_result();

//closing the statement
$stmt->close();

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

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_free_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));

//freeing the result set memory
mysqli_stmt_free_result($stmt);

//closing the statement
mysqli_stmt_close($stmt);

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

The output of the above code will be similar to:

Number of rows: 58

❮ MySQLi Functions Reference