MySQLi Tutorial MySQLi References

mysqli_result free() Method



The mysqli_result::free() / mysqli_result::close() / mysqli_result::free_result() / mysqli_free_result() function is used to free the memory associated with the result.

Syntax

//Object-oriented style
public mysqli_result::free()
public mysqli_result::close()
public mysqli_result::free_result()

//Procedural style
mysqli_free_result(result)

Parameters

result Required. For procedural style only: Specify a mysqli_result object returned by mysqli_query(), mysqli_store_result(), mysqli_use_result() or mysqli_stmt_get_result().

Return Value

No value is returned.

Example: Object-oriented style

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

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = $mysqli->query($sql);

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

//processing the data retrieved from the database
//- fetching all result rows as associative array
$rows = $result->fetch_all(MYSQLI_ASSOC);

//free the memory associated with the result
$result->free();

//displaying the rows
foreach ($rows as $row) {
  printf("%s, %d\n", $row["Name"], $row["Age"]);
}
?>

The output of the above code will be similar to:

Marry, 23
Kim, 26
John, 27
Adam, 28

Example: Procedural style

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

//getting query result from the database
$sql = "SELECT Name, Age FROM Employee ORDER BY Age";
$result = mysqli_query($mysqli, $sql);

//closing the connection
mysqli_close($mysqli);

//processing the data retrieved from the database
//- fetching all result rows as associative array
$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

//free the memory associated with the result
mysqli_free_result($result);

//displaying the rows
foreach ($rows as $row) {
  printf("%s, %d\n", $row["Name"], $row["Age"]);
}
?>

The output of the above code will be similar to:

Marry, 23
Kim, 26
John, 27
Adam, 28

❮ MySQLi Functions Reference