determine if db is open [SOVED]

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

determine if db is open [SOVED]

Post by ddragas »

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.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

What's the context, i.e. is this from within PHP? What DBMS?
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 :)
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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).
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post 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).
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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();
}
?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

If you don't have the handle, you can use mysql_ping() to ensure that the connection is still alive.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you all for killing :D and helping me out with problem I've came on 8)
Post Reply