PHP mysqli_free_result() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_free_result() frees the memory associated with a mysqli_result. After you finish reading rows, call it so the result buffer can be released.

The object-oriented method is $result->free() (also free_result() / close() on mysqli_result). Procedurally the function name is mysqli_free_result(). Once freed, do not fetch from that result again.

Syntax

Call mysqli_free_result() with the values you need. Start with this line.

PHP
mysqli_free_result($result);
CallWhat it does
mysqli_free_result($result)Frees the result set memory.
$result->free()OO form that does the same job.
mysqli_query(...); /* read rows */; mysqli_free_result($result);Typical pattern: query, fetch, then free.

This is a mysqli_result API. Large result sets benefit most from freeing early. PHP also cleans up at request end, but freeing promptly is clearer and helps long-running scripts.

Library declarations
PHP
mysqli_free_result(mysqli_result $result): void

Parameters

ParameterDescription
resultRequired. The mysqli_result you are finished using.

Return Value

Return ValueDescription
voidNo value is returned.

PHP Examples

Example 1 — Free After Reading Rows

Fetch product names, then free the result before closing the connection. Output is a fixed sample.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");
$result = mysqli_query($mysqli, "SELECT name FROM products LIMIT 2");

while ($row = mysqli_fetch_assoc($result)) {
    echo $row["name"], PHP_EOL;
}

mysqli_free_result($result);
mysqli_close($mysqli);

How It Works

  1. The query returns a mysqli_result.
  2. Rows are printed with fetch_assoc().
  3. mysqli_free_result() releases the result memory.
  4. mysqli_close() then closes the connection.

Example 2 — Object Style free()

Call $result->free() after a single-row fetch in OO style.

PHP
<?php
$mysqli = new mysqli("localhost", "demo", "secret", "shop");
$result = $mysqli->query("SELECT email FROM users WHERE id = 1");
$row = $result->fetch_assoc();

echo $row["email"], PHP_EOL;
$result->free();
$mysqli->close();

How It Works

  1. new mysqli() opens the demo connection.
  2. query() returns a result object.
  3. After reading the row, free() releases it.
  4. close() ends the connection.

Example 3 — Free Between Two Queries

Free the first result before running a second query that needs a clean slate on some workflows.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo", "secret", "shop");

$result = mysqli_query($mysqli, "SELECT COUNT(*) AS c FROM products");
$row = mysqli_fetch_assoc($result);
echo "products=", $row["c"], PHP_EOL;
mysqli_free_result($result);

$result = mysqli_query($mysqli, "SELECT COUNT(*) AS c FROM users");
$row = mysqli_fetch_assoc($result);
echo "users=", $row["c"], PHP_EOL;
mysqli_free_result($result);

mysqli_close($mysqli);

How It Works

  1. The first COUNT query is fetched and freed.
  2. A second query reuses the same connection.
  3. Its result is also freed after reading.
  4. Sample counts are printed for both tables.

Frequently Asked Questions

It frees the memory used by a mysqli_result after you finish reading it.
Call $result->free(). mysqli_result also offers free_result() and close() as aliases for the same idea.
PHP frees results at the end of the request, but calling it yourself is good practice—especially for large results or long-running scripts.
No. It only releases that result set. Use mysqli_close() or $mysqli->close() to close the connection.
No. After mysqli_free_result() the result is no longer usable. Run the query again if you need the rows once more.

Next: mysqli_get_charset()

mysqli_get_charset() returns a character-set object describing the current connection char.

mysqli_get_charset() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful