When using PHP for MySQL database operations, developers usually pay more attention to the query results themselves, such as the number of rows returned or whether the execution is successful, and it is easy to ignore a useful debugging tool - the mysqli::$info attribute. After some types of SQL statements are executed, this property can return more statistics about the execution results, helping us to understand the impact of a query more comprehensively.
mysqli::$info is a read-only property of the mysqli class, used to provide some statistical information of the last executed SQL statement, and is especially suitable for modified statements such as INSERT , UPDATE , and LOAD DATA .
The return value of this property is a string, and the content format is not fixed, but it usually contains information such as the number of affected lines, the number of repeated lines, the number of warning lines, and other information. For example:
$mysqli = new mysqli("localhost", "user", "password", "database");
$mysqli->query("UPDATE users SET status = 'active' WHERE last_login > NOW() - INTERVAL 30 DAY");
echo $mysqli->info;
The following information may be output:
Rows matched: 15 Changed: 15 Warnings: 0
This tells us that a total of 15 records were matched, 15 records were modified, and no warning occurred.
mysqli::$info returns a formatted string. The following are common return keywords:
Records : The number of records processed (mostly used in LOAD DATA )
Duplicates : The number of duplicate records (common in INSERT ... ON DUPLICATE KEY UPDATE )
Warnings : Number of warnings that appear during execution
Rows matched : Number of rows to match (usually used for UPDATE or DELETE )
Changed : The number of rows that are actually changed (indicates that the field value actually changes)
For example:
$mysqli->query("INSERT INTO logs (event, created_at) VALUES ('login', NOW()), ('logout', NOW())");
echo $mysqli->info;
The output may be:
Records: 2 Duplicates: 0 Warnings: 0
This indicates that two records were inserted, with no duplication and no warning.
By combining mysqli::$info with the logging system, we can conduct a detailed analysis of the impact of database operations. For example:
$mysqli->query("UPDATE products SET stock = stock - 1 WHERE id = 101");
$log = sprintf("Query info: %s", $mysqli->info);
file_put_contents("/var/log/db_ops.log", $log . PHP_EOL, FILE_APPEND);
This way, the number of affected rows can be recorded every time the inventory is updated, which helps with post-debugging and performance optimization.
It makes sense only after supported statements : SELECT queries usually return empty strings.
It cannot be replaced by an error handling mechanism : it is not an error reporting tool, but a supplementary information about the impact of execution.
When executing a multi-statement, only the last statement is reflected : if you use multi-statement query, it can only reflect the execution of the last one.
Language version differences : The structure of the output string may be slightly different between MySQL and PHP versions.
mysqli::$info is a simple but practical debugging tool that provides detailed statistics when executing non-query SQL statements. It provides important reference data for debugging, logging and query optimization. In applications that are highly performant or sensitive to data changes, using it rationally can significantly improve the observability of the system.
For more official documentation about this property, please refer to: https://gitbox.net/php/mysqli.info