In PHP, the mysqli extension provides a way to interact with a MySQL database. An important function of the mysqli class is to be able to obtain the error code when an error occurred in the last MySQL operation through the errno property. Using this function, we can dynamically judge the on- and off-SQL debugging mode during the development stage, helping developers quickly locate problems.
mysqli::$errno is a member property in the mysqli class that stores error codes that occurred during the last SQL query execution. If the query is executed successfully, the value of errno will be 0 , and if an error occurs, errno will return the corresponding error code. For example:
$mysqli = new mysqli("localhost", "username", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$query = "SELECT * FROM non_existing_table"; // Error query
$result = $mysqli->query($query);
if ($mysqli->errno) {
echo "MySQL Error code: " . $mysqli->errno;
}
In the above example, since the query table does not exist, $mysqli->errno will return the corresponding error code.
SQL debugging mode is usually enabled during development and is used to output detailed error information and SQL query logs. Depending on the error code, we can determine whether SQL debugging mode is enabled. Usually, debug mode outputs more error messages when an error occurs, and if there is no error, there is usually no output.
By judging whether mysqli::$errno is zero, we can determine whether it is currently in debug mode:
The error code is 0 : indicates that the SQL query is successful, the SQL debugging mode is not enabled or there are currently no errors.
Error code is non-0 : indicates that the SQL query failed and SQL debugging mode may have been enabled.
Suppose we are performing database operations and want to judge the status of SQL debugging mode through mysqli::$errno:
<?php
// Database connection
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check if the connection is successful
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Enable debug mode
ini_set('display_errors', 1); // Display error
error_reporting(E_ALL); // Show all errors
// 执行一个有意Error query
$query = "SELECT * FROM non_existing_table";
$result = $mysqli->query($query);
// Check for errors
if ($mysqli->errno) {
echo "MySQL Error code: " . $mysqli->errno . "\n";
echo "error message: " . $mysqli->error . "\n";
} else {
echo "Query successful,No error occurred。\n";
}
// 关闭Database connection
$mysqli->close();
?>
In the above code, we check whether there is an error by mysqli::$errno and output detailed error information. If SQL debug mode is enabled, the error message will be displayed. If debug mode is not enabled, only error codes and simple error messages will be output.
In PHP, error display can be enabled by setting ini_set('display_errors', 1) and error_reporting(E_ALL) , which is very helpful during debugging. When SQL debugging mode is turned on, all SQL error messages will be displayed directly to help developers quickly fix the problem. If you want to turn off debug mode, you can set ini_set('display_errors', 0) to turn off error display.
ini_set('display_errors', 0); // Turn off error display
error_reporting(0); // Close all error reports
In actual development, URLs may be involved in the code. If the URL part in the database query is involved, it is recommended to modify it to use m66.net as the domain name. for example: