Home > Database > Mysql Tutorial > body text

Can PHP Log Errors Directly to a MySQL Database?

Mary-Kate Olsen
Release: 2024-11-06 08:02:03
Original
331 people have browsed it

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;
}
Copy after login

To activate the custom error handler:

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

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!