Current Location: Home> Latest Articles> Developing and debugging tools: Real-time monitoring of mysqli::get_warnings output

Developing and debugging tools: Real-time monitoring of mysqli::get_warnings output

gitbox 2025-05-26

What is mysqli::get_warnings?

mysqli::get_warnings is a method provided by the MySQLi extension of PHP to obtain warning information generated by recently executed SQL statements. It returns a chain of mysqli_warning objects, each containing a warning details such as error codes and warning messages.

By traversing these warnings, developers can capture details that are not explicitly thrown but may affect the correctness of the data.


Basic ideas for monitoring database warnings

  1. After executing the SQL query, call get_warnings to check whether there is a warning.

  2. Traverse the warning chain and collect warning information.

  3. The warning information is output or saved in real time for convenience of subsequent analysis.

  4. It can be combined with the log system or front-end display to form a visual monitoring tool.


Sample code

The following example demonstrates how to execute a SQL statement and obtain warning information through mysqli::get_warnings and output it to the debug log.

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

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

// Example SQL:Inserting a duplicate primary key may generate a warning
$sql = "INSERT INTO test_table (id, name) VALUES (1, 'Alice')";

// Execute a query
if ($mysqli->query($sql) === TRUE) {
    echo "Query execution successfully\n";

    // Get a warning
    if ($warnings = $mysqli->get_warnings()) {
        do {
            echo "Warning code: " . $warnings->errno . "\n";
            echo "Warning message: " . $warnings->message . "\n";
        } while ($warnings = $warnings->next());
    } else {
        echo "No warning\n";
    }
} else {
    echo "Query execution failed: " . $mysqli->error . "\n";
}

$mysqli->close();
?>

Extension of real-time monitoring warnings

1. Automatically write log files

To persist warning information, you can write warnings to a file:

 function logWarnings($mysqli) {
    if ($warnings = $mysqli->get_warnings()) {
        $logFile = 'db_warnings.log';
        do {
            $log = date('Y-m-d H:i:s') . " - Warning {$warnings->errno}: {$warnings->message}\n";
            file_put_contents($logFile, $log, FILE_APPEND);
        } while ($warnings = $warnings->next());
    }
}

Automatic recording can be achieved by calling logWarnings($mysqli) after each database operation.

2. Real-time display of front-end page

Calling PHP scripts through AJAX returns the latest warnings. The front-end page can update the warning list in real time, making it easier for developers to view.


Things to note

  • Warnings are only related to the most recent database operation, and the old warnings will be overwritten after the new statement is executed.

  • mysqli::get_warnings is only valid when MySQL server supports warning mechanisms.

  • Frequent queries and recording warnings may have a certain impact on performance. It is recommended to enable only when debugging the environment or exception monitoring.


Summarize

Using PHP's mysqli::get_warnings function, you can effectively obtain warning information in database operations. Combining logging and real-time presentation technology, a simple database warning monitoring tool can be built to help developers discover potential problems in a timely manner and optimize database access and application performance.