Page 1 of 1

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

Posted: Tue Apr 10, 2007 11:55 am
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?

Posted: Tue Apr 10, 2007 12:08 pm
by Oren
Show us your code. I don't see why the if() won't work.

Posted: Tue Apr 10, 2007 12:20 pm
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;
}

Posted: Tue Apr 10, 2007 12:35 pm
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());
}
?>

Posted: Tue Apr 10, 2007 12:35 pm
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);

Posted: Tue Apr 10, 2007 12:41 pm
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. :)

Posted: Tue Apr 10, 2007 1:41 pm
by RobertGonzalez
Just make sure to turn display_errors to off in a production environment.

Posted: Tue Apr 10, 2007 1:43 pm
by Oren
Dale, didn't you mean: "[SOLVED]"?