Databases Reference
In-Depth Information
"/tmp/php_errors.log");
exit;
}
The first parameter to the error_log( ) function is the message string. The second
parameter is the type of logging we want; 3 means write to the specified file. The last
parameter is the path to the logfile. In building up the message, we've used the
date( ) function to get the current timestamp (for example, 2006.07.17_04:34:27) and
then append the error message to this. The logfile would contain error messages like
this:
2006.07.17_04:38:00: Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (2)
2006.08.05_11:52:37: Unknown column 'artist' in 'where clause'
Ideally, you wouldn't want to regularly check the logfile to learn about problems. You
can ask the error_log( ) function to send you an email for each error:
// Define the email address separately from the code, making it easier to maintain.
// This line can be placed in a separate configuration file.
define("ADMINISTRATOR_EMAIL_ADDRESS", "support@learningmysql.com");
// Custom error handler function
function showerror($connection)
{
// Display a message to the user
echo "<h3>Failure</h3>
Unfortunately we're having technical difficulties; this has been logged.
Please try again later.";
// Create message with the current timestamp and the MySQL error.
$sMessage= date("Y.m.d_H:i:s").": ".mysqli_error($connection)."\n";
// Log the timestamp and error description to the specified email address
error_log(
$sMessage,
1,
ADMINISTRATOR_EMAIL_ADDRESS);
exit;
}
Here, the second parameter is set to 1 , indicating that we want to send an email, and
the third parameter is the destination email address. In case you were wondering, using
0 for the second parameter writes the message to the default log specified in the
php.ini file, while using 2 writes the message to a TCP port for use with a PHP debugging
tool. We don't think either of these options is particularly useful for you at this stage.
Note that we've used the define( ) function to define the constant ADMINISTRA
TOR_EMAIL_ADDRESS outside the body of the function. This allows us to specify the email
address somewhere easy to access and modify (perhaps in a header file), rather than
having to search complex code for the email address.
 
Search WWH ::




Custom Search