In daily PHP development, database connection errors are common but troublesome issues. Especially when using the MySQLi extension, the error messages are often unclear, making it difficult to quickly pinpoint the source of the problem. To enhance debugging efficiency, mysqli::$errno and mysqli_report() are particularly important. This article will dive into their functions and usage methods, helping developers quickly identify and resolve database connection errors.
mysqli::$errno is a property in the object-oriented interface of MySQLi, used to return the error code generated by the most recent database operation. If no error occurs, the return value is 0. By checking this error code, developers can more clearly understand the type of error, such as whether it's a failed authentication, a non-existent database, etc.
$mysqli = new mysqli("localhost", "user", "wrong_password", "database");
<p>if ($mysqli->connect_errno) {<br>
echo "Connection failed, error code: " . $mysqli->connect_errno . "\n";<br>
echo "Error message: " . $mysqli->connect_error . "\n";<br>
// Handle the error based on the error code<br>
if ($mysqli->connect_errno === 1045) {<br>
echo "Hint: Please check if the database username or password is correct.";<br>
}<br>
}<br>
In the example above, if the password is incorrect, error code 1045 is returned, indicating authentication failure. By checking this error code, we can provide specific feedback to the user or developer to correct the issue.
mysqli_report() is a configuration function used to set the error reporting behavior of the MySQLi extension. By default, MySQLi errors only return a boolean or null value, and developers must manually check each operation. However, once error reporting is enabled, MySQLi will throw warnings or exceptions, thus simplifying the debugging process.
MYSQLI_REPORT_OFF: Turns off all error reporting (default).
MYSQLI_REPORT_ERROR: Reports errors.
MYSQLI_REPORT_STRICT: Throws errors as exceptions (recommended).
MYSQLI_REPORT_ALL: Reports all errors, including syntax warnings.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<p>try {<br>
$mysqli = new mysqli("localhost", "user", "wrong_password", "database");<br>
// Perform an erroneous query<br>
$mysqli->query("SELECT * FROM non_existent_table");<br>
} catch (mysqli_sql_exception $e) {<br>
echo "An error occurred: " . $e->getMessage() . "\n";<br>
echo "Error code: " . $e->getCode() . "\n";<br>
// Further log or report the error<br>
file_put_contents("/var/log/db_errors.log", $e->getMessage(), FILE_APPEND);<br>
}<br>
This approach not only simplifies the code logic but also helps quickly identify where the error occurred, making it ideal for use during the development phase.
In real-world projects, it is recommended to enable strict error reporting in development or testing environments (using mysqli_report()), while combining mysqli::$errno for more detailed error checks when catching exceptions.
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
<p>try {<br>
$mysqli = new mysqli("localhost", "user", "password", "database");<br>
echo "Database connection successful";<br>
} catch (mysqli_sql_exception $e) {<br>
echo "Database connection failed, error message: " . $e->getMessage();</p>
if (strpos($e->getMessage(), '1049') !== false) {
echo "\nHint: The database does not exist, please check the database name.";
} elseif (strpos($e->getMessage(), '1045') !== false) {
echo "\nHint: Incorrect username or password, please check the authentication information.";
}
}
You can also combine custom logging systems to record error information locally or to a remote log service, for example:
$logData = date('Y-m-d H:i:s') . " Database error: " . $e->getMessage() . "\n";
file_put_contents("https://m66.net/logs/db_errors.log", $logData, FILE_APPEND);
Note: The URL domain in the example above has been replaced with m66.net, please adjust the remote logging logic based on the actual situation.
By properly using mysqli::$errno and mysqli_report(), PHP debugging efficiency can be significantly improved, enabling quick identification and localization of errors when database connection issues occur. It is recommended to always enable strict mode (MYSQLI_REPORT_STRICT) in development environments, and to pair it with exception handling mechanisms in production environments for safe degradation or log recording.
Debugging is not the goal; stability is the objective. By making good use of these two small tools, your PHP database operations will become more controllable, measurable, and maintainable.