What happens if the MySQL database has not been closed?

What happens if the MySQL database is not closed? How do we know if it is closed properly?

I have a page on which there are 11 tables on the page .. so what I did, I open the database on top of the page before the script starts and closes where the script ends (PHP) ...

the end is mysql_close ($ db); ... is this fair enough or do I need to give only mysql_close ();

+2
php mysql
Jun 02 '10 at 13:20
source share
2 answers

I can’t say for sure that all versions of PHP / Mysql on all server platforms behave the same. For tcp connections to the database - if you do not call mysql_close ($ db), you will have a tcp dangling connection that sits there, expecting it to be used for half a minute after the script ends. Then he just leaves on his own.

I can’t tell if this is a PHP garbage collection filling up the full 30 seconds, or if the tcp connection expires 30 seconds after you call the connection.

Mysql_close ($ db) instantly kills the tcp connection. So yes, I would say always call mysql_close ($ db) immediately after you no longer need the database connection in the script.

+3
Jun 02 '10 at 18:52
source share

There is no connection close point at the end of the script, because this is done for you when the script exits. The only time you really need to call the close function explicitly is if you are using a daemon or something and don’t want to hold the connection for the entire execution length.

PDO is considered a modern database access library for PHP (you really should use this instead of mysql_* functions, but that's a different story), and even this doesn't have a close function, just to guide the point at home.

-one
Jun 02 '10 at 13:29
source share



All Articles