Welcome Guest, Not a member yet? Register   Sign In
Freeing database resources
#1

[eluser]-sek[/eluser]
Does the idiom

return $this->db->query($sql, array($id, $date, $text));

automatically free db resources? I've seen this used and have used it myself for returning the query object from a model, but I am curious as to whether it leaves resources open without being explicitly freed.

Thanks,

Steve
#2

[eluser]-sek[/eluser]
No one?

I understand that PHP will free database resources when the code execution ends. I understand that CI may free resources after it completes. The exception being, as the documents imply, when more than one query is run.

So if I have

query1

query2

and they do not free resources each time, they will leave resources open until all the code executes.

The question is, when I use

return [db query object]

I assume it does not free the resource and the caller would have to free it after it was through with the result.

Or instead of returning the db query object, I'd have to write in the model function

$query = $this->db->query($sql, array($id, $date, $text));
$query->free_result();
return $query;

Do I have this correct?
#3

[eluser]TheFuzzy0ne[/eluser]
You assume correctly. The resource is not freed unless you explicitly make it so.

Freeing the result before using it won't do anything but give you a headache. The result should be freed after you've used it. You're model might be better off like this:

Code:
$query = $this->db->query($sql, array($id, $date, $text));
$ret = $query->result_array(); # Grab the result array.
$query->free_result(); # Now we free the result as we have our results in an array.
return $ret; # return the array.

Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB