tags:

views:

69

answers:

1

Hi,

I am aware of that All associated result memory is automatically freed at the end of the script's execution. But would you recommend using it, if I am using a quite of lot of somewhat similar actions as below?

$sql = "select * from products";
$result = mysql_query($sql);
if($result && mysql_num_rows($result) > 0) {
  while($data = mysql_fetch_assoc($result)) {
     $sql2 = "insert into another_table set product_id = '".$data['product_id']."'
              , product_name = '".$data['product_name']."'
             ";
     $result2 = mysql_query($sql2);
     **mysql_free_result($result2);**  
  }
}

Thanks.

+3  A: 

Quoting the documentation of mysql_free_result :

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.
All associated result memory is automatically freed at the end of the script's execution.


So, if the documentation says it's generally not necessary to call that function, I would say it's not really necessary, nor good practice, to call it ;-)

And, just to say : I almost never call that function myself ; memory is freed at the end of the script, and each script should not eat too much memory.
An exception could be long-running batches that have to deal with large amounts of data, though...

Pascal MARTIN