FarmerG
FarmerG

Reputation: 11

Storing an Error log in MySQL

I want to store my the errors logs my PHP creates into a MySQL table, but nothing is being inserted into it.

the code I'm using for the error log is..

function myHandler($code, $msg, $file, $line) 
   {
      echo "An error occurred while processing your request. Please visit our site and try again.";  
      // log error to file, with context
      $logData = date("d-M-Y h:i:s", mktime()) . ", $code, $msg, $line, $file\n";
      die("Error");
   }

with the lines

$insert = "INSERT INTO error_logs (error_message)"."VALUES ('$logData')";   
mysql_query($insert)or die(); 

to insert the log into the error_message column in the error_logs table which is set to VARCHAR with 250 character cap

I assumed it'd just set a something in there, but apparently I'm way off the mark.

Upvotes: 1

Views: 902

Answers (1)

Naftali
Naftali

Reputation: 146360

You insert statement could parse to:

INSERT INTO error_logs (error_message) VALUES ('foo')

Which is invalid.

Make sure to put that space in there.

You might want to put the MySQL error statement into the die(..) so you can better see the error next time.

Also – please please please escape your data before inputting it into a statement.... The way you are doing it now can lead some big Bobby Tables issues...

Please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun on it. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Upvotes: 2

Related Questions