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(). Однако, использовать её крайне не рекомендуется.



mysql_free_result
Nairebis
25-Feb-2006 06: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 04: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
02-Jul-2005 12: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 02: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
01-Sep-2004 04: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 03: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.

<mysql_field_typemysql_get_client_info>
 Last updated: Tue, 15 Nov 2005