[solved]how to check if connected to mySQL database

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
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

[solved]how to check if connected to mySQL database

Post 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...
Last edited by vigge89 on Thu Jan 15, 2004 8:28 am, edited 1 time in total.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post 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";
//...
?>
Last edited by scorphus on Tue Jan 13, 2004 10:53 am, edited 1 time in total.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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)
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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....
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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 :)
Post Reply