Page 1 of 1

mysqli_close()-when&where to use

Posted: Mon Mar 14, 2011 8:47 pm
by El Vasco
Hi All,
My question today has to do with the way in which mysqli_close() has to be used... After having written quite a lot lines and functions of php code interacting with mysql database I realized that I was not using mysqli_close().

My questions are:
Do all mysqli_connect() have to have their correspondent mysqli_close() ? If Yes, when and where do you have to include it ? At the end of the script ? Right after the query to the database was run ? other ?

Any hint about the propper use of mysqli_close() it will be very much appreciated :-) !

Thanks !
El Vasco

Re: mysqli_close()-when&where to use

Posted: Mon Mar 14, 2011 9:06 pm
by gooney0
Well it should be used when you no longer want to use the connection. In other words you have no more queries to run.

I haven't run across any issues with leaving it out. You might have a problem if you needed to connect to two different databases.

I just put it at the bottom of my logic script. You don't want tot put it in your functions as it would be a waste to connect and close over and over again.

Now mysqli_free_result() should be used as soon as you are done with the result handle. This you'll want use in your functions before you return.

Re: mysqli_close()-when&where to use

Posted: Tue Mar 15, 2011 4:18 pm
by El Vasco
Thank You for your response, Gooney !
Honestly speaking I have not been using mysqli_close() and I did not see any adverse result, neither... Anyway, I guess I will follow your advise and use it at the end of the script...