Using MySQL databases is a common operation when developing PHP applications. In PHP, we often use the mysqli class to interact with the MySQL database. With mysqli , we can easily perform database queries, get results and perform other database operations. However, when actually writing database manipulation code, we may ignore some details, causing the code to go wrong in certain specific situations.
Among them, a common problem is that before accessing the mysqli::$errno property, forget to check whether the database connection is successful. Let's take a look at what problems will cause if the connection is not successfully checked.
Mysqli::$errno is a very useful property when performing database operations using mysqli . It represents the error code for the previous database operation. When you perform operations such as query or insertion, if an error occurs, errno will return an integer value indicating the error type. If no error occurs, its value will be 0.
For example:
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
echo "Query error: " . $mysqli->errno . " - " . $mysqli->error;
}
In the above example, $mysqli->errno is used to get the error code and process it.
Usually, we will check whether the database connection is successful before each database operation, for example, to judge by the connect_error attribute. If the database connection fails, but we do not check the connection status and directly perform the query operation, it will cause serious errors.
Consider the following code:
$mysqli = new mysqli("localhost", "user", "password", "database");
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
echo "Query error: " . $mysqli->errno . " - " . $mysqli->error;
}
In this example, we do not determine whether mysqli is successfully connected to the database. If the database connection fails, $mysqli->query() will not be executed, and $mysqli->errno will not contain the correct error message, which may return 0 or other unrelated values, resulting in the inability to accurately locate the problem.
Specifically, the following are the problems that may arise:
If the connection is not checked for success before executing the query, the program may continue to execute, resulting in an incorrect errno value. For example, if the database connection fails, the error code does not reflect the actual situation of the connection failure, but rather reflects the error of the query operation, which may obfuscate the developer's debugging process.
If we do not check when the connection fails, the program may not give a clear error message, causing us to be unable to quickly discover the problem. For example, a connection error may be ignored and the program will continue to execute like a normal query until other irrelevant errors occur. This situation often makes developers lose themselves in a series of chain reactions, making it difficult to find the real source of the error.
In some cases, if the connection status is not checked first, the error message may reveal sensitive information related to the database. For example, the error message may contain database connection strings or other sensitive data. Through the error handling method, an attacker may have the opportunity to obtain information about the system structure, which may even lead to the emergence of security vulnerabilities.
To avoid the above problems, we should check whether the connection is successful before performing any database operations. Here is a correct code example:
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
$query = "SELECT * FROM table";
$result = $mysqli->query($query);
if ($mysqli->errno) {
echo "Query error: " . $mysqli->errno . " - " . $mysqli->error;
}
In this code, we first check whether the connect_error is empty to determine whether the database connection is successful. If the connection fails, the program will terminate early and an error message will be displayed. If the connection is successful, the query operation continues and when querying, use $mysqli->errno correctly to check for errors.
Forgot to check if the connection is successful before performing a database operation can lead to multiple problems such as wrong error codes, hidden connection errors, and even potential security issues. To avoid these problems, be sure to first confirm that the connection is successful before each database operation. This will make the program more robust and error information more accurate, while improving the maintainability and security of the code.
I hope this article helps you understand why you must first determine whether the connection is successful before using mysqli::$errno , and avoid problems caused by ignoring this detail.