Mysql_query frees memory when a local variable no longer exists?

When using mysql_query inside a class method and setting it to a local variable, memory is freed when the method completes execution or at the end of the script? Should I add mysql_free_result ? The following is an example.

 class example{ public function Query($query){ $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $this->rows[] =$row; } } } 

Will memory be freed when the method completes, or is it necessary to call mysql_free_result at the end of the method?

+4
source share
1 answer

First of all, mysql_ php functions mysql_ no longer supported, and so you can use a more OOP approach like mysqli or PDO .

Since mysql_free_result()

free all memory associated with the result of the result identifier.

If you are extracting a lot of results, then yes, you can (and should) use this function to free up your mysql to save memory.

As for the rest of your question:

Will memory be freed when the method completes, or is it necessary to call mysql_free_result at the end of the method?

No, in case you do not use mysql_free_result , the memory will not be freed at the end of your method, but rather at the end of your script.

Hope this helps. Greetings.

+3
source

Source: https://habr.com/ru/post/1445287/


All Articles