Current Location: Home> Latest Articles> How to get warning information for the last operation through mysqli::get_warnings

How to get warning information for the last operation through mysqli::get_warnings

gitbox 2025-06-03

When using MySQL database in PHP, we often pay attention to whether the query is successfully executed, but sometimes although the query is successfully executed, it may be accompanied by warning information, such as data truncation, field type mismatch, etc. These warning messages are often ignored, but in fact they are very useful for troubleshooting problems and optimizing database operations.

PHP's mysqli extension provides a method get_warnings() , which is specifically used to obtain warning information generated by the latest database operation. This article will introduce the usage scenarios and specific usages of this method in detail to help you better capture and handle database warnings.


What is mysqli::get_warnings?

mysqli::get_warnings() is a method of the mysqli class that returns a mysqli_warning object that contains all warnings generated by the latest database operation. By traversing this object, you can get the error code and details for each warning.

This method is particularly suitable for SQL statements that have potential problems although execution is successful, such as data truncated during insertion or update, using an incomplete matching field type, or performing certain MySQL functions but returning non-fatal warnings.


How to use mysqli::get_warnings()

The following is a simple example to demonstrate the use of mysqli::get_warnings() . Suppose you insert extra long data into a string field that defines a length limit, a warning will be triggered.

 <?php
// Connect to the database
$mysqli = new mysqli('gitbox.net', 'username', 'password', 'testdb');

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

// Create a sample table
$mysqli->query("CREATE TABLE IF NOT EXISTS test_warning (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(5)
)");

// Insert extra long string,Trigger warning
$mysqli->query("INSERT INTO test_warning (name) VALUES ('Test strings beyond length')");

// Get warning information
$warnings = $mysqli->get_warnings();

if ($warnings) {
    do {
        printf("Warning code: %d, information: %s\n", $warnings->errno, $warnings->message);
    } while ($warnings->next());
} else {
    echo "无警告information。\n";
}

// Close the connection
$mysqli->close();
?>

In the above code:

  • We inserted a string that exceeds the field length limit. MySQL will perform the insertion but truncate the string, resulting in a warning.

  • Use $mysqli->get_warnings() to get the warning object.

  • By calling $warnings->next() on a loop, iterate through all warnings and output details.


When to use mysqli::get_warnings()

  • Warning check when data is inserted or updated <br> When you have strict requirements on the validity of your data, warning messages can help you detect potential data loss or format errors.

  • Debugging complex SQL operations <br> Some complex queries or stored procedures may return warnings, and using this method can help debugging.

  • Logging and monitoring <br> Automatically grab warnings and write them to the log, and assist operation and maintenance personnel to discover database exceptions in a timely manner.


Summarize

mysqli::get_warnings() is a very practical tool to help you capture and deal with warnings that do not affect the results of the operation but may affect the quality of the data. Making it rationally can make your application more robust and stable.

For more tips on using PHP and MySQL, visit https://gitbox.net for the latest tutorials and examples.