Page 1 of 1

[solved]how to check if connected to mySQL database

Posted: Tue Jan 13, 2004 10:32 am
by vigge89
i want to check if the script is connected to mysql as eas as possible, no querys or stuff. Is there an variable set, a function to check or something. I just wanna know if there is anything like this before i continue with my script, becuase i don't want to query the mysql-database just to check if im connected...

Posted: Tue Jan 13, 2004 10:51 am
by scorphus
[url=http://www.php.net/mysql#mysql.examples]MySQL Functions[/url] section of the PHP Manual wrote:

Code: Select all

<?php
   /* Connecting, selecting database */
   $link = mysql_connect("mysql_host", "mysql_user", "mysql_password")
       or die("Could not connect : " . mysql_error());
   echo "Connected successfully";
//...
?>

Posted: Tue Jan 13, 2004 10:52 am
by infolock
just put an error check after your connect status..

ie

Code: Select all

mysql_connect('localhost','username','password') or die(mysql_error());
echo 'Connected to MySQL Server!';
if you are not connected, it will error out and give you the error.

otherwise, it will perform whatever else is after the connect (in this case an echo statement)

Posted: Wed Jan 14, 2004 9:54 am
by vigge89
i know that, but I have a file (or, 1 included in the other), which needs to check if the script is already connected to the mysql database, by the other file....

Posted: Wed Jan 14, 2004 5:44 pm
by microthick
When you connect to the mysql db, you could always set a global variable to 1. Then before connecting ever again, check to see the value of the global variable. This assumes that no accidental disconnections from mysql will occur.

Posted: Thu Jan 15, 2004 2:53 am
by twigletmac
Depending on what exactly you are trying to do it may not be necessary to check:
PHP Manual - mysql_connect() wrote: If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.
Mac

Posted: Thu Jan 15, 2004 8:28 am
by vigge89
twigletmac wrote:Depending on what exactly you are trying to do it may not be necessary to check:
PHP Manual - mysql_connect() wrote: If a second call is made to mysql_connect() with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned.
Mac
ok, thanks :)