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?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
[Fixed]How to determine when PHP cannot connect to database?
Moderator: General Moderators
[Fixed]How to determine when PHP cannot connect to database?
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..
Last edited by Dale on Tue Apr 10, 2007 12:42 pm, edited 1 time in total.
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;
}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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());
}
?>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():
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:
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());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.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():
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.Code: Select all
<?php $conn = mysql_connect("DBHOST", "USERNAME", "PASSWORD") or die("Won't connect"); mysql_select_db("DATABASE", $conn) or die(mysql_error());
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);
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA