Home  >  Article  >  Database  >  Can PHP Log Errors Directly to a MySQL Database?

Can PHP Log Errors Directly to a MySQL Database?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-06 08:02:03337browse

Can PHP Log Errors Directly to a MySQL Database?

Logging PHP Errors to a Database Instead of error_log

Is it feasible to configure PHP to log errors directly to a MySQL database rather than the default error_log file? Although implementing a custom error handler is a viable approach, this would necessitate significant code modifications. Is there a more global solution?

Answer:

Unfortunately, it is not possible to accomplish this without creating a custom error handler. However, this custom error handler would effectively serve as the desired "one global change."

The modified example from the PHP manual demonstrates how this can be achieved:

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
     // Establish a connection to the MySQL database here
     mysql_query("INSERT INTO error_log (number, string, file, line) ".
                 "VALUES .....");         

    /* Suppress PHP internal error handler from executing */
    return true;
}

To activate the custom error handler:

// Assign the custom error handler
$old_error_handler = set_error_handler("myErrorHandler");

The above is the detailed content of Can PHP Log Errors Directly to a MySQL Database?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn