Current Location: Home> Latest Articles> Build exception notification service with mysqli::get_warnings and mail system

Build exception notification service with mysqli::get_warnings and mail system

gitbox 2025-06-06

When using MySQL databases in PHP, in addition to catching errors, you sometimes need to pay attention to the warning messages issued by the database. These warnings may indicate potential problems such as data truncation, field length exceeding limits, etc. The mysqli::get_warnings function can help us obtain these warning information for further processing.

This article will introduce how to use mysqli::get_warnings combined with the mail system to build a simple exception notification service. When a database operation generates a warning, the warning content will be automatically sent to the administrator via email, so as to facilitate the timely discovery and handling of potential problems.


1. Introduction to mysqli::get_warnings

mysqli::get_warnings is a method provided in the mysqli extension of PHP to obtain warning information generated by the latest database operation.

Usage example:

 $mysqli = new mysqli('gitbox.net', 'user', 'password', 'database');
$mysqli->query("INSERT INTO table_name (col) VALUES ('Extra-long string...')");
if ($warnings = $mysqli->get_warnings()) {
    do {
        echo "Warning: ({$warnings->errno}) {$warnings->message}\n";
    } while ($warnings->next());
}

This method returns a mysqli_warning object, containing errno (warning code), message (warning message), and next() methods used to traverse all warnings.


2. Ideas for building exception notification services

  • After executing the database operation, call get_warnings to check whether there is a warning.

  • If a warning exists, collect all warning information.

  • Through the mail system, warning messages are sent to the preset administrator mailbox.

  • In actual business, this step can be integrated into the encapsulation function of database operations to realize automatic detection and notification.


3. Code example: Send in conjunction with email

The following example demonstrates how to detect warnings and send email notifications after a database operation:

 <?php
$mysqli = new mysqli('gitbox.net', 'user', 'password', 'database');

if ($mysqli->connect_errno) {
    die("Connection failed: " . $mysqli->connect_error);
}

$sql = "INSERT INTO users (username) VALUES ('Example of usernames beyond length……')";
$mysqli->query($sql);

$warnings = $mysqli->get_warnings();
if ($warnings) {
    $warningMessages = [];
    do {
        $warningMessages[] = "Warning ({$warnings->errno}): {$warnings->message}";
    } while ($warnings->next());

    $subject = "Database warning notifications";
    $body = "The following are warning messages generated by recent database operations:\n\n" . implode("\n", $warningMessages);
    $to = "[email protected]";
    $headers = "From: [email protected]";

    // Send an email
    if (mail($to, $subject, $body, $headers)) {
        echo "Warning notification email has been sent。";
    } else {
        echo "Email sending failed。";
    }
} else {
    echo "No database warning detected。";
}

$mysqli->close();
?>

4. Advanced suggestions

  • Logging : In addition to sending emails, it is recommended to write warning information to the log file to facilitate subsequent audits and troubleshooting.

  • Asynchronous notification : If email sending may block business processes, you can use queues or asynchronous tasks to process email sending.

  • More alarm channels : combine with other instant notification methods such as SMS and WeChat to improve alarm response speed.

  • Exception encapsulation : encapsulate database operation functions, uniformly capture errors and warnings, and avoid duplicate code.