Reputation:
How would I write a MySQL error to a file instead of displaying it to the user?
here is what I have so far...
if (!mysql_query($sql_query,$connection))
{
die('Error: ' . mysql_error());
}
echo "Success!";
Upvotes: 1
Views: 3898
Reputation: 5833
You can use the error_log function in php for that.
error_log("You messed up!", 3, "/var/tmp/my-errors.log");
Edit: so in your case this would look like the following (although i would use a different if statement)
if (!mysql_query($sql_query,$connection))
{
error_log(mysql_error() . "\n", 3, "/var/tmp/my-errors.log");
}
echo "Success!";
Upvotes: 6
Reputation: 6632
Use error_log, or fopen/fwrite/fclose/etc.
I often use create my own error handler with something like set_error_handler in PHP and use trigger_error to capture ALL errors and write them to file. This may be a better scenario for you; rather than writing numerous error_log()'s, you can just create an error handler function and then use trigger_error.
Upvotes: 1
Reputation: 881
Firstly, you should not use die if you do not want to display your error message to the user.
Secondly, instead of using die, you must log your error message into a file. If you are using some logging library, you may output the error to some log, else you may want to have a look at File handling in PHP.
Link - http://davidwalsh.name/basic-php-file-handling-create-open-read-write-append-close-delete
Upvotes: 0