1

In a previous ticket i asked about logging PHP errors in MySQL which gives me:

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
         // mysql connect etc here...
    $sql = "INSERT INTO `error_log` SET
            `number` = ".mysql_real_escape_string($errno).",
            `string` = ".mysql_real_escape_string($errstr).",
            `file` = ".mysql_real_escape_string($errfile).",
            `line` = ".mysql_real_escape_string($errline);
    mysql_query($sql);
    // Don't execute PHP internal error handler
    return true;
}

// set to the user defined error handler 
$new_error_handler = set_error_handler("myErrorHandler");

I can make this work but only if it is triggerred like this:

trigger_error("message here");

However, I also want the error handler to be called for all errors such as syntax errors like:

echo "foo;

But these errors are just outputted to the screen, what am i doing wrong?

Community
  • 1
  • 1
robjmills
  • 18,438
  • 15
  • 77
  • 121
  • Can I ask why you want to log such errors? – Chris Aug 19 '10 at 19:25
  • 1
    You can't handle non-catchable fatal errors with a php error handler (And a syntax error is a non-catchable fatal error). It just can't be done. – ircmaxell Aug 19 '10 at 19:30
  • I was wanting to capture any error within my system without needing to make any other changes. At the moment a syntax error would mean that nothing is reported at all currently – robjmills Aug 19 '10 at 19:32
  • possible duplicate of [How do I catch a PHP Fatal Error](http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error) – ircmaxell Aug 19 '10 at 19:41
  • @Col. Shrapnel - care to explain why its a "terrible way of error logging" ? – robjmills Aug 19 '10 at 20:00

3 Answers3

2

You can only handle runtime errors with a custom error handler. The echo "foo error in your example happens when parsing (i.e. reading in) the source. Since PHP can not fully parse the code, it can also not run your error handler on this error.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
1

If You're forced to test if syntax is correct, You can use php_check_syntax function, with filename parameter PHP Manual php_check_syntax

php_check_syntax also provides second parameter, witch when used will be populated by the error string, as far as i remember

canni
  • 5,737
  • 9
  • 46
  • 68
  • I ofen use php_check_syntax in magic autoload function, when code is in development, it helps a lot in many problems, uncatchable in some circumstances (frameworks error handling is far from good) :) – canni Aug 19 '10 at 19:49
0

That's indeed terrible way of error logging

  1. You don't need not a single advantage of a database. Would you make a database lookup for the certain line number? Or order your results by file name?
  2. database is a subject of many errors itself.
  3. You've been told already that it's impossible to catch a parse error at the program logic level, because a syntactically wrong program will never run.

Let's take your code as an example. It will raise a MySQL error (because of poorly formed query) which you will never see. As well as any other errors occurred. That's what I am talking about.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345