deltanovember deltanovember - 7 days ago 5
PHP Question

How do I check if PHP is connected to a database already?

Basically in pseudo code I'm looking for something like

if (connected_to_any_database()) {
// do nothing
}
else {
mysql_connect(...)
}


How do I implement

connected_to_any_database()

Answer

Have you tried mysql_ping?:

mysql_ping()

Checks whether or not the connection to the server is working. If it has gone down, an automatic reconnection is attempted.

or second approach (but mysql_ping is more reliable in this case):

$link = mysql_connect('localhost','username','password');
//(...)
if($link == false){
    //try to reconnect
}