Any Point in Using mysql_close()

I don’t use mysql_close() in php after making database request

Is there any point to using it doing while doing a insert, or update query like

$query = "INSERT INTO $table VALUES ('','$widget1','$widget2')"; 
$result = mysql_query($query);

Im guessing proper usage would be


What is the point of it, is there any point? Does anyone use it? If so/not why?


that would be a php question.

i’d guess without it you are leaving the database call open and there are limits of how many can be open/connected at one time.

Yeah i guess it is, but i figured some mysql nut would be better able to school me in the finer points of mysql :smiley:

Maybe someone will move it to the php area.

Thanks for the reply guelphdad

well they would, except it has nothing to do with mysql per se. It is a command in php application and doesn’t exist within mysql itself. php of course being one of many front end interfaces you can use.

OK, well thanks for your help either way.

Maybe a mod will see this and move it :slight_smile:

Off Topic:

If you ever want one of your threads moved, don’t hesitate to use to bring it to a moderator’s attention

What the close() is closing is a resource. How important is it? If the script is short and doesn’t use much memory or open lots of resources, probably not enough to worry about, It will close when the script ends.

But if the code is involved and does a lot, why not use it? It can’t hurt any by closing it once you’re certain you’re done with it. Similar is using unset(). Generally no big deal, but if you’re keeping a lot of temporary data in memory in a longer script, it should atleast be considered.

Unless the part of the script that runs after you are finished with the database executers millions of lines of code it will not make a noticeable difference because the resource is released when the PHP finishes anyway so if that is soon after you finish with the database then the extra time will be insignificant.

The only time you’d expect to have a script running for that long afterwards is if it is a background script. Any script loading a web page would be abandoned by your visitor before the page finishes loading if the script runs for long enough after finishing with the database to make it worth closing the connection.

Close can also be important if (for some bizarre reason) you’re using the outdated mysql module’s persistant connection option… but as has been said by the others, unless you’re running a script of an obscenely long time, PHP’s cleanup process at the end of the script will take care of it without concern.

That reminds me of a real life experience. I wrote a plugin for WordPress and after thousands of downloads without much complaint, someone reported a MySQL “bug”. He was running PHP 4 and an old version of MySQL (and an old version of WordPress too, gotta wonder). So I optimized it as best I could even though IMHO he should have found a better host.

The point being if you’re not writing code for your own use, you may want to give close() and unset() deeper thought.

Hey this ended up being a good post. :slight_smile:

Thanks you guys.

so guess the consensus is
use it it can’t hurt, and it essentially clears any memory usage of mysql that may be created by large scripts or older outdated version of mysql.

:slight_smile: Awesome, and thanks.

If you are running a persistent connection then the close does nothing and so can definitely be omitted in that instance. The close command only works if it isn’t a persistent connection.