In PHP, mysqli is a very common database extension library used to operate MySQL databases. And mysqli::$errno is the error code used to get the most recent MySQL error. If you accidentally use the wrong scope to call it, it may lead to misjudgment errors and affect the stability and correctness of the program. Today we will discuss this common problem to help everyone avoid traps.
mysqli::$errno is a property that belongs to the mysqli class. It is used to return the error code that occurred in the last operation (such as querying, connecting, executing SQL statements, etc.). If there is no error, the return value is 0 .
Usually, mysqli::$errno is used with other MySQL error messages. For example, mysqli::$error returns the error message corresponding to the error code.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
// implement SQL Query
$result = $mysqli->query("SELECT * FROM non_existent_table");
if ($mysqli->errno) {
echo "SQL Error code: " . $mysqli->errno . "\n";
echo "error message: " . $mysqli->error . "\n";
}
?>
In this example, if the query table does not exist, $mysqli->errno will return a non-zero error code, indicating an error occurred.
The scope of the error usually refers to the fact that you call mysqli::$errno in the wrong context. The most common thing about this is when using a database connection instance ( $mysqli ), if you do not initialize the connection correctly or access this property in an incorrect object context, it may result in wrong error codes or error messages.
<?php
// Incorrect use mysqli::$errno
$mysqli = new mysqli("localhost", "user", "password", "database");
// Use the wrong scope
if ($mysqli->errno) {
echo "SQL Error code: " . $mysqli->errno . "\n";
echo "error message: " . $mysqli->error . "\n";
}
?>
In this example, if SQL operations have not been performed (such as executing a query), the value of $mysqli->errno will be 0 , but if the program does not handle this correctly, it may be mistaken for an error. You would think that the error code of mysqli::$errno is because a query failed and may not actually execute the query at all.
To avoid misjudgment, it is recommended that the value of mysqli::$errno is checked only after performing SQL operations. That is, mysqli::$errno only makes sense after database operations (such as query or insert).
For example:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_error) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
// implement SQL Query
$result = $mysqli->query("SELECT * FROM non_existent_table");
if ($result === false) {
echo "SQL Error code: " . $mysqli->errno . "\n";
echo "error message: " . $mysqli->error . "\n";
}
?>
In this code, we only check mysqli::$errno when the query operation $mysqli->query() returns false (indicating failure). If you check the SQL operation without performing it, it may lead to misjudgment.
If you check mysqli::$errno before performing any SQL operations, it will return 0 , which means there is no error. However, if you mistakenly think that this is a valid error code, it may cause the program to enter the error handling logic and even make subsequent database operations unpredictable.
To give a simple example, if we use the following error code, no error actually occurs, but due to improper scope of the inspection, the program will misjudgment and error handling:
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
// Error check
if ($mysqli->errno) {
echo "Error code: " . $mysqli->errno;
} else {
echo "No errors";
}
?>
In this case, although the program does not have any errors, the output may be "no errors" due to the scope of the error, which leads to misjudgment.
Mysqli::$errno can be checked correctly only after performing SQL operations.
Mysqli::$errno has a value of 0 before performing any database operations, and checking it at this time does not get a valid error message.
Proper scope use can help you avoid misjudgments and ensure the robustness of your program.
Hopefully this article can help you understand why calling mysqli::$errno with the wrong scope will cause misjudgment, and give some suggestions to prevent errors. If you have any other questions about PHP or database, please visit our website!