Forum Moderators: open

Message Too Old, No Replies

check if connected to mysql database

         

kristof_v

2:51 pm on Nov 22, 2006 (gmt 0)

10+ Year Member



hi

how can you check if you already have a conenction to a mysql database?
the php function mysql_ping() looks promising but when you are not connected it tries to conenct for you.
that is not wanted, i just want to check if there is already an open connection to mysql.

preferrably in a php script

grtz

FalseDawn

5:27 pm on Nov 22, 2006 (gmt 0)

10+ Year Member



Why do you need to do this? Just curious.

The preferred method for accessing mysql is to open a connection at the start of the script, then close it at the end - I can't really see a need to check if a connection exists.

kristof_v

8:19 am on Nov 23, 2006 (gmt 0)

10+ Year Member



hi FalseDawn,

opening a connection at the start of a script and closing it in the end is what i do now.
and it works great, but i fear that i may have forget a close somewhere or do a erdirect before closing in any of all the scripts.

it was just meant for testing purposes.

eelixduppy

12:44 pm on Nov 23, 2006 (gmt 0)



Put this right before you want to work with the database.


$link = mysql_connect('localhost','username','password');
if($link == FALSE)
{
echo 'Cannot connect to the database';
[url=http://us2.php.net/manual/en/function.exit.php]exit[/url]();
}

Best of luck :)

FalseDawn

5:41 pm on Nov 23, 2006 (gmt 0)

10+ Year Member




but i fear that i may have forget a close somewhere or do a erdirect before closing in any of all the scripts

I wouldn't worry too much about it, PHP's reference counting and garbage collector will take care of it, even if you forget.

[us2.php.net...]