RE: [PHP] Close A Databse Connection
| From: | Peter J. Schoenster | Date: | Fri, 26 Jul 2002 21:02:13 +0000 |
| Subject: | RE: [PHP] Close A Databse Connection | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-109884@lists.php.net to get a copy of this message | ||
Hi,
The question was about the use of mysql_close. Use it or not (not when to use it or when not to). So
far:
> Agreed... you should always "clean up after yourself."
> PHP will close the connection automatically will be closed and all
> information cleared.
> I advised a newbie to
> always use mysql_close() to close the connection to the database. It's
> good form and will cover any ills where the database connection fails to
> close for some other reason (say the user stops the page from loading
> after the connection is made).
2 says use it
1 says no.
From my read of docs I'd say not to use it. I use the Perl DBI with Apache::DBI and it simply
overrides the DBI disconnect so calling a disconnect is only a waste of minimal time and typing
when using it.
So, in PHP there are also 2 types of connections:
mysql-pconnect
mysql_connect
Well if you are using mysql-pconnect then you would "rarely" use mysql_close() as the docs
say
Using mysql_close() isn't usually necessary, as non-persistent open links are automatically
closed at the end of the script's execution. See also freeing resources.
__end quote
Okay, so when, when using a persistent connection would I use mysql_close()?
http://www.php.net/manual/en/function.mysql-pconnect.php
says
"Second, the connection to the SQL server will not be closed when the execution of the script
ends. Instead, the link will remain open for future use (mysql_close() will not close links
established by mysql_pconnect())."
See the above url for information from user comments about cutting down idle connections.
I think it's safe to say one would not use mysql-close() when using mysql-pconnect, although I
wonder about the cost of doing that.
http://www.php.net/manual/en/function.mysql-close.php
says
> Using mysql_close() isn't usually necessary, as non-persistent open
> links are automatically closed at the end of the script's execution.
It seems to me better to err on the side of caution and use mysql_close(). If you are using a
persistent connection then the call to mysql_close() should be overridden but I've got no clue
if it's
done that way. Seems to be related to opening and closing a file. In Perl the file will close when
the script exits and that's fine for quick stuff but once a script begins to grow it allows for
bugs.
I vote to suggest use of mysql_close().
Peter