Current Location: Home> Latest Articles> Forgot to release the result set: Why use mysqli_result::free()

Forgot to release the result set: Why use mysqli_result::free()

M66 2025-05-31

When using PHP to operate MySQL databases, many developers are accustomed to using mysqli extensions to perform database operations. However, many people ignore one detail: after query. You might ask, is $result->free() (or mysqli_free_result() ) really that important? Why can't it be omitted? This article will be discussed in detail.

What is a result set?

When you use mysqli_query() to execute a query statement, for example:

 $mysqli = new mysqli("localhost", "username", "password", "database");
$result = $mysqli->query("SELECT * FROM users");

The $result here is a mysqli_result object, which stores all the data returned by the query. It consumes memory resources , especially when the query results are large, memory consumption increases significantly.

Why release the result set?

Many people think that PHP automatically manages memory and will automatically clean up variables when the script ends. But the fact is:

? The memory will be automatically cleaned when the script ends.
? But during the script running, if you have multiple queries or large queries and do not release them actively, the memory pressure will become greater and greater.

For example:

 for ($i = 0; $i < 1000; $i++) {
    $result = $mysqli->query("SELECT * FROM big_table");
    // No result set is released
}

In the above code, each loop will generate a result set object, but it is not released. This will keep memory rising, which can eventually lead to memory overflow or performance drops sharply .

If you add $result->free() :

 for ($i = 0; $i < 1000; $i++) {
    $result = $mysqli->query("SELECT * FROM big_table");
    $result->free();
}

In this way, at the end of each loop, the memory occupied by the result set will be released in time, greatly reducing the memory usage.

How to correctly release the result set?

There are two ways:

1?? Object-oriented style:

 $result = $mysqli->query("SELECT * FROM users");
if ($result) {
    // Processing results...
    $result->free();
}

2?? Process style:

 $result = mysqli_query($mysqli, "SELECT * FROM users");
if ($result) {
    // Processing results...
    mysqli_free_result($result);
}

Note: If the query fails (returns false ), calling free() will report an error, so you must first determine whether $result is a valid object.

What happens if it is not released?

You may not feel the difference with short scripts and single query. However, in these scenarios, there will be serious consequences for not releasing the result set:

  • Big data query: When querying tens of thousands or hundreds of thousands of rows, the result set occupies a lot of memory.

  • Long-running scripts: such as timed tasks, crawlers, and daemons.

  • High concurrent web service: Each request consumes memory and drags down the server for a long time.

In these cases, the result set is not released, and the application can be slowed down at the least, and the server can be crashed at the worst.

summary

Remember one sentence: Used resources must be released manually!
Even if PHP recycles memory automatically, don't rely on it to manage running resources.

In actual development, developing good habits - $result->free() is timely after querying, not only writing "right" code, but also writing "good" code.