determine if db is open [SOVED]
Moderator: General Moderators
determine if db is open [SOVED]
how to determine if db is open after connection
Last edited by ddragas on Sat Jun 03, 2006 8:05 am, edited 2 times in total.
By rule, the connection will be closed once the script stops execution, (as long as it's not a persistent connection) so there is really no reason to close the connection anyway. If you have multiple database connections per script, which isn't very likely, then you might have reason to close each when you're done with it to save system resources. BTW, the function you're looking for is aptly named mysql_close().ddragas wrote:
db=mysql
and it is within PHP
script is:
connect to db and select table
execute some query
if connection to database is open then close it
OTOH, closing an open connection or resource handler, setting variables to null, etc are all signs of Good Practice, yet not strictly necessary (especially in a quick PHP script).
Hmm. I must say that's the oddest bit of advice on a PHP forum I've ever come across.Weirdan wrote:if you want a man to be dead, you just shoot him in the head. What's the point of checking if he was alive prior to shooting?
if you want to close db connection, just do it
If you think about it, though, logic dictates that you must check to see if he was alive before shooting, if in fact the actual motive to shoot was to kill. You can't kill a dead man, although you can shoot one (not that I endorse either).
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Getting back to OP's question...
Code: Select all
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if ($conn)
{
// Yeah, I am connected
}
// later on, toward the end of the script
// If we are still connected, disconnect
if ($conn)
{
mysql_close();
}
?>- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US