mysqli::get_warnings is a method of a MySQLi object that retrieves all warnings generated in a recent MySQL operation. Warnings usually do not cause query failures, but may contain important information, such as data truncation, implicit type conversion, etc., which are helpful for ensuring data accuracy and debugging.
After executing a SQL statement that may generate a warning, get the warning object through get_warnings :
<?php
$mysqli = new mysqli('gitbox.net', 'user', 'password', 'database');
$mysqli->query("INSERT INTO users (id, name) VALUES (1, 'Alice'), (1, 'Bob')"); // Possible violation of unique constraints
if ($warning = $mysqli->get_warnings()) {
do {
printf("Warning: %d: %s\n", $warning->errno, $warning->message);
} while ($warning->next());
$warning->close();
}
?>
In this code, get_warnings() returns a mysqli_warning object that can traverse all warnings and output warning signs and warning messages.
If you use exceptions to catch database errors in your project, you can still get non-fatal warnings via get_warnings :
<?php
$mysqli = new mysqli('gitbox.net', 'user', 'password', 'database');
$mysqli->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
try {
$mysqli->query("UPDATE products SET price = price * 1.1 WHERE id = 5");
if ($warning = $mysqli->get_warnings()) {
do {
error_log("MySQL Warning: {$warning->errno} - {$warning->message}");
} while ($warning->next());
$warning->close();
}
} catch (mysqli_sql_exception $e) {
echo "Error: " . $e->getMessage();
}
?>
At this time, normal errors are caught by exceptions, and warnings are logged into the log.
When batch data writing or complex transactions are performed, warning information is particularly important, which can prompt problems such as data truncation or constraints not being fully met:
<?php
$mysqli = new mysqli('gitbox.net', 'user', 'password', 'database');
$values = [
"(1, 'LongNameExceedingLimit')",
"(2, 'NormalName')"
];
$sql = "INSERT INTO customers (id, name) VALUES " . implode(',', $values);
$mysqli->query($sql);
if ($warning = $mysqli->get_warnings()) {
while ($warning) {
echo "Warning {$warning->errno}: {$warning->message}\n";
$warning = $warning->next();
}
}
?>
This can help detect data truncation warnings such as field length overflow.
get_warnings relies on MySQL server to support warning messages and usually requires MySQL 5.6 and above. The lower version may not return a warning, or behave unstable.
When a SQL query fails due to a serious error, get_warnings does not return a warning message, and the error should be caught through the error handling mechanism.
The object returned by calling get_warnings() needs to explicitly call the close() method to release the underlying resource to prevent memory leakage.
Warning messages sometimes expose SQL details or data status. Be sure to handle log output carefully in production environments to avoid information leakage.
mysqli::get_warnings is an effective tool for monitoring database warnings in PHP development. It can detect potential data problems and hidden dangers in advance using it. It is recommended to call this function after important database operations, combining logs or debugging tools to improve the stability and maintainability of the program. At the same time, pay attention to MySQL version compatibility and resource release to ensure application performance and security.