0

I have a custom error handler that's supposed to log errors to a database, but for some reason this error always shows up in the database. Every time a page is loaded "0 in on line 0", where the first 0 is the error level, in whatever file, since it's not provided, on line 0. This is odd because I've never seen this until now. The error handler is below;

public function fatalErrHandlr(){
    $errstrArr = error_get_last();
    $errno = mysqli_real_escape_string($this->dbc, trim($errstrArr['type']));
    $errstr = mysqli_real_escape_string($this->dbc, trim($errstrArr['message']));
    $errfile = mysqli_real_escape_string($this->dbc, trim($errstrArr['file']));
    $errline = mysqli_real_escape_string($this->dbc, trim($errstrArr['line']));
    $query = "INSERT INTO `err` (`errno`, `errstr`, `errfile`, `errline`) VALUES ('$errno', '$errstr', '$errfile', '$errline')";
    mysqli_query($this->dbc, $query);
    //var_dump(mysqli_error($this->dbc));
    echo("<b>There was an error. Check the database.</b>");
    //return true;
}

The error handler is configured with:

register_shutdown_function(array($core, 'fatalErrHandlr'));
6
  • How are you calling the error handler? Commented Aug 24, 2015 at 21:54
  • 1
    BTW, learn to use prepared queries instead of escaping all the values. Commented Aug 24, 2015 at 21:54
  • It seems like you're calling the error handler in cases where there was no error. Commented Aug 24, 2015 at 21:55
  • @Barmar normal queries work just fine, and I was under the impression that the error handler just needed to be set like so; register_shutdown_function(array($core, 'fatalErrHandlr')); on the index page. Commented Aug 24, 2015 at 22:00
  • @Barmar in that case I got false information. I thought that it ran every time there was an error after the script shut down. In that case, if the type value of the array is zero, i can have it not enter anything into the db Commented Aug 24, 2015 at 22:08

1 Answer 1

1

register_shutdown_function doesn't configure an error handler, it sets a function to run whenever the script finishes. So you're getting a log message every time the script runs, whether or not it got an error. The correction function is set_error_handler. It passes the details as arguments to the callback, so you don't need to use error_get_last

public function fatalErrHandlr($errno, $errstr, $errfile, $errline){
    $query = $this->dbc->prepare("INSERT INTO `err` (`errno`, `errstr`, `errfile`, `errline`) VALUES (?, ?, ?, ?)");
    $query->bind_param('ssss', $errno, $errstr, $errfile, $errline)
    $query->execute();
    //var_dump(mysqli_error($this->dbc));
    echo("<b>There was an error. Check the database.</b>");
    //return true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Refer to my comment on the original question. This did lead me to an answer, though. I will accept this as the answer to the question.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.