mysql_free_result() Function for PHP

What is mysql_free_result() function in PHP?
How does mysql_free_result() works?

Explanation

Mysql function mysql_free_result() will free all memory associated with the result identifier result.
Syntax
bool mysql_free_result ( resource result)

This mysql_free_result() returns TRUE on success, or FALSE on failure.
mysql_free_result() frees the memory used by a result handle, deleting the result handle in the process. mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets. In most cases, this function is unnecessary; PHP's memory-management system automatically releases the memory used by result handles at the end of the script execution.
Example:

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
mysql_select_db("my_database");
//execute simple query
$result = mysql_query("SELECT * FROM mytable");
$row = mysql_fetch_array($result,MYSQL_BOTH);
//print result
print_r($row);
//free the result resource
mysql_free_result($result);
?>

RESULT:
Array (
[0] => 76 [id] => 76
[1] => Nicolus [name] => Nicolus
[2] => selected [status] => selected
)

In the above code mysql_free_result() function is used to free the result handle '$result'. When the result handle is accessed after deleting the result resource using mysql_free_result() function will result in error.
Example:

<?php
//Attempt to connect to the default database server
$link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
or die ("Could not connect");
//select database
mysql_select_db("my_database");
//execute simple query
$result = mysql_query("SELECT * FROM mytable");
$row = mysql_fetch_array($result,MYSQL_BOTH);
//print result
print_r($row);
//free the result resource
mysql_free_result($result);
$row = mysql_fetch_array($result,MYSQL_BOTH);
//print result
print_r($row);
?>

RESULT:
mysql_fetch_array(): is not a valid MySQL result resource

PHP Topics


Ask Questions

Ask Question