DB Error Catching in PHP…
In line with the ‘@’ functionality which prevents PHP from splattering error messages to the user. I wrote this code which to replace the current method of accesing a DB…
The original method would be
$dB_connect=mysql_connect($dbhost,
$dbusername,$dbpasswd)
Then preceeding it with a ‘@’, the new code would look like this.
$dB_connect=@mysql_connect($dbhost,
$dbusername,$dbpasswd)
$dB_connect=@mysql_connect($dbhost,
$dbusername,$dbpasswd);if (!$dB_connect) {
error_log(mysql_error(), 3, “./logfile.log”);
require_once(’error.html’);
exit();
}
else{
if (!mysql_select_db($database_name)) {
error_log(mysql_error(), 3, “./logfile.log”);
require_once(’error.html’);
exit();
}
}
How the code works, is that if a connection error is experienced, either by wrong password or missing DB entirely. It would log the offending error message to the ‘logfile.log’ and then instead of the offending error message shown to the user, a custom made error.html file will be loaded…
It’s up to you to make the contents of ‘error.html’… hehehe
[source]