Click to See Complete Forum and Search --> : Handling a mysql_connect() error


chestertb
05-01-2007, 08:52 PM
Hi All,
How do I capture and supress the following mysql_connect error;
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'user'@'localhost' (using password: YES) in ../connect.inc.php on line xx

The connection script looks like this...
$link = mysql_connect("localhost", $connect['user'], $connect['pass']);
if(! $link)
{
//abort execution and load the "Not Connected" page using header()
header("Location: http://".$_SERVER['HTTP_HOST']."/fail.htm");
}
else
{
$connected = "Y";
}
...which is as per the php on-line documentation.

What it should do, I think, is try to connect to the database, and if the connection fails (for whatever reason), it should load the fail.htm page. It doesn't. I just get the above error, followed by more as the script continues to execute and tries to access the database.

Any suggestions?
CTB

NogDog
05-01-2007, 09:46 PM
1. You could put a "@" in front of the mysql_connect() function in order to suppress error reporting.

2. You could change the error_reporting level:

error_reporting(E_ERROR ) | E_PARSE); // only report fatal errors


3. You could use output buffering (http://www.php.net/manual/sv/function.ob-start.php).

Each has certain advantages/disadvantages, which you can choose between for yourself.