Page 1 of 1

determine if db is open [SOVED]

Posted: Thu Jun 01, 2006 6:38 am
by ddragas
how to determine if db is open after connection

Posted: Thu Jun 01, 2006 8:12 am
by bdlang
What's the context, i.e. is this from within PHP? What DBMS?

Posted: Thu Jun 01, 2006 8:19 am
by ddragas
bdlang wrote:What's the context, i.e. is this from within PHP? What DBMS?

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

Posted: Thu Jun 01, 2006 9:06 am
by Weirdan
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 :)

Posted: Thu Jun 01, 2006 7:57 pm
by bdlang
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
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().

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).

Posted: Thu Jun 01, 2006 8:00 pm
by bdlang
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 :)
Hmm. I must say that's the oddest bit of advice on a PHP forum I've ever come across. :D

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).

Posted: Thu Jun 01, 2006 8:29 pm
by RobertGonzalez
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();
}
?>

Posted: Thu Jun 01, 2006 9:47 pm
by Ambush Commander
If you don't have the handle, you can use mysql_ping() to ensure that the connection is still alive.

Posted: Fri Jun 02, 2006 11:00 am
by Weirdan
bdlang wrote:if in fact the actual motive to shoot was to kill.
if the intent was to kill - then yes. But if you just want to be sure the one is dead...

:) agreed, it's not the best metaphor.

Posted: Sat Jun 03, 2006 7:51 am
by ddragas
Thank you all for killing :D and helping me out with problem I've came on 8)