简体   繁体   中英

PHP custom error handler reporting 0 in on line 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'));

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM