1

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!";
1
  • 1
    try writing that mysql_error() to a file. how? check php file manipulation .. Commented Jul 15, 2012 at 19:02

3 Answers 3

6

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!";
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget the mysql_error() message and a \n since mode 3 doesn't break lines on its own.
1

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.

Comments

0

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

1 Comment

And you would have to use mysql_error() to get the error message which is to be written to the file!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.