[solved]how to check if connected to mySQL database
Moderator: General Moderators
[solved]how to check if connected to mySQL database
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.
- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
[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.
just put an error check after your connect status..
ie
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)
ie
Code: Select all
mysql_connect('localhost','username','password') or die(mysql_error());
echo 'Connected to MySQL Server!';otherwise, it will perform whatever else is after the connect (in this case an echo statement)
-
microthick
- Forum Regular
- Posts: 543
- Joined: Wed Sep 24, 2003 2:15 pm
- Location: Vancouver, BC
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
Depending on what exactly you are trying to do it may not be necessary to check:
MacPHP 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.
ok, thankstwigletmac wrote:Depending on what exactly you are trying to do it may not be necessary to check:MacPHP 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.