mysql_free_result

(PHP 3, PHP 4, PHP 5)

mysql_free_result -- 释放结果内存

说明

bool mysql_free_result ( resource result )

mysql_free_result() 将释放所有与结果标识符 result 所关联的内存。

mysql_free_result() 仅需要在考虑到返回很大的结果集时会占用多少内存时调用。在脚本结束后所有关联的内存都会被自动释放。

如果成功则返回 TRUE,失败则返回 FALSE

为向下兼容仍然可以使用 mysql_freeresult(),但反对这样做。


add a note add a note User Contributed Notes
Nairebis
26-Feb-2006 10:00
ALWAYS use this function! I just encountered a bug in my code where I forgot to use this function. I also happen to be using mysql_pconnect() for a persistent connection. If you forget to free the result, it can hold the old result set open indefinitely within the HTTP process.

The upshot (in my application) was that I did updates that happened in a different HTTP process, but they mysteriously didn't show up in another HTTP process. After panicking that MySQL had mysterious data corruption and/or synchronization problems, I traced it back to this where an old result set was held open.
mdeininger at jyujin dot de
20-Sep-2005 07:45
yes, i encountered that too. as far as i could tell, that's because the script is stored in memory after being compiled and that's as much more memory as it needs for a call to that function.

if you always get lotsa data in your results, using this function will decrease memory usage tho, unless you use non-buffered queries (which are preferable unless you absolutely *have* to use mysql_seek(), or you need to do another query while the last one hasn't finished reporting back, as they can provide a small speedup)
macronesia at macronesia dot net
03-Jul-2005 03:11
You not need to use this if you are using PHP 4.

The comment below this comment may explain why it's actually costing more memory.
Joachim Kruyswijk
14-Jun-2005 05:42
Using this function may actually increase the amount of memory used. In my case, the script used 208 bytes less memory when *not* using mysql_free_result().
Check for yourself: call memory_get_usage() at the end of the script.
marcelo at orionlab dot net
02-Sep-2004 07:28
A very simple example:

<?php

// author: marcelo santos araujo
// marcelo at orionlab dot net

@mysql_connect("host","user","password");
@
mysql_select_db("database");

$simple_query = @mysql_query("SELECT * FROM login;");

// using mysql_free_result()
// managing memory

@mysql_free_result($simple_query);

?>
steve at stevedix dot de
06-Feb-2003 07:58
Godyvdb below is making an incorrect assumption, which may confuse.

When you call a query, you are returned a handle, which points to a set of results and the associated variables for it.  When you call free_result with that handle it will do a garbage - collection upon the result and associated variables, freeing up the memory.

What it WON'T do, and this is what Godyvdb is assuming, is optimise memory space by freeing all memory taken up by similar queries.  In his example
 
$myhandle=mysql_query("....");
mysql_free_result(mysql_query("...."));

all he does is makes another query to the database, generates a new handle, then immediately frees up the space allocated to the result.

If you wish to free up memory, then you can only do so by quoting the correct handle. You cannot generate a new handle to an existing query by the use of mysql_query.

eg.

$myhandle = mysql_query("....");
$anotherhandle =mysql_query("....");
mysql_free_result($myhandle);

$anotherhandle will still point to an existing set of results, whilst $myhandle won't.  The two handles are not related.