[Fixed]How to determine when PHP cannot connect to database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

[Fixed]How to determine when PHP cannot connect to database?

Post by Dale »

I am just wondering would it be possible to avoid these usual messages when my host's MySQL server resets/backs up/crashes etc.. etc..
Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server during query in /mounted-storage/home37a/sub005/sc29969-WRBK/www/datab.php on line 7

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /mounted-storage/home37a/sub005/sc29969-WRBK/www/datab.php on line 8
Lost connection to MySQL server during query
I've tried doing an IF statement on the $conn value (eg; $conn = mysql_connect() ) but that doesn't work. However do you have any suggestion on to make it say something else that I set?
Last edited by Dale on Tue Apr 10, 2007 12:42 pm, edited 1 time in total.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Show us your code. I don't see why the if() won't work.
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Well I tried something like this:

Code: Select all

<?php
$conn = mysql_connect("DBHOST", "USERNAME", "PASSWORD");
mysql_select_db("DATABASE", $conn) or die(mysql_error());

if(!$conn) {
echo "The database server is not on or working";
exit;
}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
if (!$conn = mysql_connect("DBHOST", "USERNAME", "PASSWORD"))
{
    die('There as a connection problem: ' . mysql_error());
}

if (!mysql_select_db("DATABASE", $conn))
{
    die('Could not select the database: ' . mysql_error());
}
?>
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Have you considered putting the if() right after you connect to the database?

Anyway, if you want your script to exit in a case of a failure then just do what you did with the mysql_select_db():

Code: Select all

<?php

$conn = mysql_connect("DBHOST", "USERNAME", "PASSWORD") or die("Won't connect");
mysql_select_db("DATABASE", $conn) or die(mysql_error());
P.S If mysql_connect() fails then the above code will die and print "Won't connect", but the error/warning from PHP will be there too. If you want to get rid of it, set display_errors to Off on the production server. It's better be set to On on the development server.

If you don't have access and you can't change the server's configuration, then you will still be able to do it on-the-fly with:

Code: Select all

ini_set('display_errors', 0);
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post by Dale »

Oren wrote:Have you considered putting the if() right after you connect to the database?

Anyway, if you want your script to exit in a case of a failure then just do what you did with the mysql_select_db():

Code: Select all

<?php

$conn = mysql_connect("DBHOST", "USERNAME", "PASSWORD") or die("Won't connect");
mysql_select_db("DATABASE", $conn) or die(mysql_error());
P.S If mysql_connect() fails then the above code will die and print "Won't connect", but the error/warning from PHP will be there too. If you want to get rid of it, set display_errors to Off on the production server. It's better be set to On on the development server.

If you don't have access and you can't change the server's configuration, then you will still be able to do it on-the-fly with:

Code: Select all

ini_set('display_errors', 0);
It originally didn't work with the first thing, but when I added that ini_set() tag it did work, cheers dude. Oh and thanks for your input Everah. :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just make sure to turn display_errors to off in a production environment.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Dale, didn't you mean: "[SOLVED]"?
Post Reply