When using PHP’s MySQLi extension, mysqli::$errno is a very important property that holds the error code from the last database operation. Usually, we use it to determine whether the database operation was successful or to perform error handling. But there is a noteworthy point about mysqli::$errno that can cause some hard-to-detect errors. Today, we’ll delve into this issue.
mysqli::$errno is a property of the MySQLi class that returns the error code of the most recent database operation. If no error occurred, mysqli::$errno returns 0. Otherwise, it returns an error code indicating the specific type of error.
For example, when executing a query, if the query succeeds, mysqli::$errno returns 0; if it fails, it returns the corresponding error code.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
<p>if ($mysqli->connect_errno) {<br>
echo "Connection failed: " . $mysqli->connect_error;<br>
exit();<br>
}</p>
<p>$query = "SELECT * FROM non_existent_table";<br>
$mysqli->query($query);</p>
<p>if ($mysqli->errno) {<br>
echo "Query failed, error code: " . $mysqli->errno;<br>
}<br>
?><br>
In the example above, if the query fails, $mysqli->errno will return a number representing the error.
The problem lies in the fact that mysqli::$errno does not automatically clear before each new database operation. In some cases, the error code may be retained, causing subsequent database operations to be mistakenly interpreted as failures. This can be very confusing because even if the following operations do not produce errors, $mysqli->errno still returns the previous error code.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
<p>$query1 = "SELECT * FROM valid_table";<br>
$mysqli->query($query1);</p>
<p>$query2 = "SELECT * FROM non_existent_table";<br>
$mysqli->query($query2);</p>
<p>// At this point, $mysqli->errno may still return the error code from the previous query, not the current one<br>
if ($mysqli->errno) {<br>
echo "Query failed, error code: " . $mysqli->errno;<br>
}<br>
?><br>
In the code above, even though the second query fails (querying a non-existent table), $mysqli->errno might not immediately show the correct error code. It may actually display the error code from the first query (if there was one), causing a misjudgment.
To avoid this pitfall, the best practice is to manually reset mysqli::$errno to 0 before every new query or database operation. You can clear all error codes using the mysqli::clear_errors() method.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
<p>$query1 = "SELECT * FROM valid_table";<br>
$mysqli->query($query1);</p>
<p>// Manually clear errors<br>
$mysqli->clear_errors();</p>
<p>$query2 = "SELECT * FROM non_existent_table";<br>
$mysqli->query($query2);</p>
<p>if ($mysqli->errno) {<br>
echo "Query failed, error code: " . $mysqli->errno;<br>
}<br>
?><br>
This way, you ensure that $mysqli->errno is clean before each query, preventing old error codes from interfering with new operations.
mysqli::$errno is a very useful property in MySQLi that helps us identify and handle database errors. However, it has a hidden pitfall: it does not reset automatically. This means that if you do not manually clear the error code, it may persist and affect error handling in subsequent operations. To avoid this, it is recommended to use the mysqli::clear_errors() method before each new database operation to manually clear the error code.
Related Tags:
mysqli