mysqli::$info is a read-only property that returns the detailed information generated by the recently executed query. It is usually used to provide information such as the number of lines affected, warnings, etc. after the UPDATE , DELETE , or INSERT ... ON DUPLICATE KEY UPDATE statement.
$mysqli = new mysqli('gitbox.net', 'user', 'password', 'database');
$mysqli->query("UPDATE users SET status = 'active' WHERE last_login > NOW() - INTERVAL 30 DAY");
echo $mysqli->info;
This property is only valid after a specific type of query, and only returns meaningful information after the query is successfully executed.
This is the most common problem. mysqli::$info will only be set after certain specific queries, such as the UPDATE statement. If the query produces no additional information, $mysqli->info will return NULL or empty string.
Solution:
Before using, check the query type to confirm its applicability. It is also recommended to use it in conjunction with $mysqli->affected_rows to ensure that you have obtained the correct feedback.
$result = $mysqli->query("UPDATE users SET status = 'active' WHERE last_login > NOW() - INTERVAL 30 DAY");
if ($result) {
echo "Number of affected rows: " . $mysqli->affected_rows . "\n";
echo "Additional information: " . ($mysqli->info ?? 'No information') . "\n";
} else {
echo "Query failed: " . $mysqli->error;
}
If the query fails to execute, mysqli::$info still exists, but its content is untrusted. If you do not judge the query results and access them directly, you may get meaningless or incorrect information.
Solution:
Always judge whether the query is successful first, and then visit mysqli::$info .
if ($mysqli->query($sql) === false) {
echo "mistake: " . $mysqli->error;
} else {
echo "information: " . $mysqli->info;
}
If the character set for the database connection is incorrectly set, mysqli::$info may return garbled or incomplete information.
Solution:
Make sure that the correct character set is set immediately after connecting to the database, for example:
$mysqli->set_charset('utf8mb4');
This ensures that the string returned by mysqli::$info is displayed correctly.
mysqli::$info is the result of the last successful query. If another query is executed in the middle, accessing the property may return the results of the previous query, causing misunderstanding.
Solution:
Make sure that no other irrelevant queries are inserted before each access to mysqli::$info , or only accessed immediately after the relevant queries.
Use exception handling : combined with mysqli_report (MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT) , it can catch query errors earlier and avoid misunderstanding of error information.
Query return value judgment : Not only mysqli::$info judges query results, but it is best to combine affected_rows and error for comprehensive judgment.
Document reference : Learn more about the official documentation's description of mysqli::$info , and understand the scenarios and limitations it applies to.
In summary, mysqli::$info is a practical but misusable property. Correctly judge the query results and use them in combination with other mysqli attributes can effectively avoid errors during use.
$mysqli = new mysqli('gitbox.net', 'user', 'password', 'database');
$mysqli->set_charset('utf8mb4');
$sql = "UPDATE users SET status = 'active' WHERE last_login > NOW() - INTERVAL 30 DAY";
if ($mysqli->query($sql) === false) {
echo "查询mistake: " . $mysqli->error;
} else {
echo "Number of affected rows: " . $mysqli->affected_rows . "\n";
echo "Additional information: " . ($mysqli->info ?? 'No information') . "\n";
}