Which PHP function is used to release cursor memory associated with MySQL result?

PHP uses mysql_free_result() function to release the cursor memory associated with MySQL result. This function is part of the original MySQL extension (now deprecated) and returns no value.

Syntax

mysql_free_result(result);

Parameters

Parameter Description
result Required. Specifies a result set identifier returned by mysql_query(), mysql_store_result() or mysql_use_result()

Example

Here's how to use mysql_free_result() to free memory after processing a MySQL query result −

<?php
$connection = mysql_connect("localhost", "username", "password");
mysql_select_db("database_name", $connection);

$query = "SELECT * FROM users";
$result = mysql_query($query);

// Process the result
while($row = mysql_fetch_array($result)) {
    echo $row['name'] . "<br>";
}

// Free the memory associated with the result
mysql_free_result($result);

mysql_close($connection);
?>

Modern Alternative

Since the original MySQL extension is deprecated, use mysqli_free_result() with MySQLi or PDO instead −

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

while($row = $result->fetch_assoc()) {
    echo $row['name'] . "<br>";
}

// Free the memory
$result->free();
$mysqli->close();
?>

Conclusion

The mysql_free_result() function releases memory associated with MySQL result sets. However, use mysqli_free_result() or PDO for new projects since the original MySQL extension is deprecated.

Updated on: 2026-03-15T07:24:04+05:30

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements