mysqli_close()-when&where to use

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

mysqli_close()-when&where to use

Post 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
gooney0
Forum Commoner
Posts: 56
Joined: Fri Jan 21, 2011 1:40 pm
Location: Reston, VA

Re: mysqli_close()-when&where to use

Post 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.
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

Re: mysqli_close()-when&where to use

Post 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...
Post Reply