Forum Moderators: coopster
php_value error_reporting <<INTEGER SEE LINK IN PREVIOUS OPTION>>
php_flag log_errors on
error_reporting(<<CONSTANT SEE LINK IN FIRST OPTION>>);
error_reporting = E_ALL
display_errors = Off
log_errors = On
error_log = [file_name] $con = mysql_connect("","","");
if(!conn) {
trigger_error("Connection failed" . mysql_error())
} $con = mysql_connect("", "", "");
if(!$con) {
echo "<p>User-friendly message for user.</p>";
error_log("MySQL connection failed! Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personaladdress@example.com", $headers);
}
else {
// Rest of code
} <?php
// Create a function that throws an exception.
function handle_mysql_error($err) {
throw new Exception($err);
}
// Code to try. If it fails, it will be caught by the 'catch' block.
try {
$conn = mysql_connect("localhost", "user", "password") or handle_mysql_error("Connection failed! ");
mysql_select_db("database") or handle_mysql_error("Select database failed! ");
// Select data from database.
$sql = "SELECT * FROM `table`";
$query = mysql_query($sql, $conn) or handle_mysql_error("Query failed! ");
}
catch(exception $e) {
// Set appropriate header for bots.
header('HTTP/1.1 503 Service Temporarily Unavailable');
// Initialise user-friendly message to be displayed in body for user.
$userErrorMessage = "<p>Sorry, this service is temporarily unavailable. The webmaster has been notified.</p>";
// Log the error in the error log file.
trigger_error($e->getMessage() . mysql_error(), E_USER_WARNING);
// Send yourself an email.
error_log($e->getMessage() . "Date: " . date("l jS \of F, Y, h:i:s A") . ". File: " . $_SERVER['REQUEST_URI'], 1, "personalemail@example.com", "From: help@mysite.com");
}
?> if(isset($userErrorMessage)) {
echo $userErrorMessage;
}
else {
while($row = mysql_fetch_assoc($query)) {
// Do whatever.
}
} <?php
if(trim(shell_exec('cat /var/log/httpd/mysql.log | grep -m 1 "' . date('Y-m-d') . '"')) != '') {
mail('admin@example.com', 'MySQL errors found', 'MySQL errors found on site example.com', "From: cron@example.com");
}
?>
I've checked it and it works perfectly. However, one problem remains: If there's an error and the site's very popular, I will be sent thousands of e-mails in a short period of time!
How can I prevent that from happening? Any ideas will be much appreciated.