Current Location: Home> Latest Articles> Will $errno overwrite when multiple queries? How to use it correctly?

Will $errno overwrite when multiple queries? How to use it correctly?

M66 2025-05-17

When using PHP for database operations, the mysqli extension provides functionality for query execution. With a database connection, we can execute multiple SQL queries, but if you execute multiple queries, you may encounter a problem - will mysqli::$errno be overwritten? In other words, when multiple queries share a connection, how do you correctly get the error code for each time?

In this post, we will discuss the question of whether mysqli::$errno will be overwritten and give a solution to how to get the error code correctly between multiple queries.

1. What is mysqli::$errno ?

mysqli::$errno is a property in the mysqli class that returns the error code of the last query. The error code is a number that indicates the status of the current operation. If the value is 0, no error occurred.

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$query = "SELECT * FROM non_existent_table";
$mysqli->query($query);

echo "Error code: " . $mysqli->errno;
?>

In the above code, $mysqli->errno will return the error code for the last query.

2. Will mysqli::$errno be overwritten when multiple queries share one connection?

If you execute multiple queries on the same connection, mysqli::$errno will be updated to a new error code after each query. So, if you call $mysqli->errno after a query and then execute the next query, $mysqli->errno will be overwritten by the new error code.

For example:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$query1 = "SELECT * FROM non_existent_table";
$query2 = "SELECT * FROM another_non_existent_table";

$mysqli->query($query1);
echo "第一个查询Error code: " . $mysqli->errno . "<br>";

$mysqli->query($query2);
echo "第二个查询Error code: " . $mysqli->errno . "<br>";
?>

When running the above code, $mysqli->errno will return the first error code after executing the first query, but when executing the second query, it will be overwritten by the new error code. Therefore, you can only see the error code for the last query, and you cannot directly get the error code for each query.

3. How to correctly obtain the error code for each time?

In order to correctly obtain the error code for each query, you can manually save the current error code after each query is executed. A variable can be used to record the error code for each query, thereby avoiding the problem of being overwritten.

Here is the improved code:

 <?php
$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

$query1 = "SELECT * FROM non_existent_table";
$query2 = "SELECT * FROM another_non_existent_table";

// Execute the first query
$mysqli->query($query1);
$error_code1 = $mysqli->errno; // 保存第一个查询的Error code
echo "第一个查询Error code: " . $error_code1 . "<br>";

// Execute the second query
$mysqli->query($query2);
$error_code2 = $mysqli->errno; // 保存第二个查询的Error code
echo "第二个查询Error code: " . $error_code2 . "<br>";
?>

In this example, we use $error_code1 and $error_code2 to save the error code for each query, respectively. In this way, the error code of each query will not be overwritten, but will be recorded separately.

4. Conclusion

By using different variables to save the error code for each query, you can ensure that even if multiple queries share a connection, the error code will not be overwritten. This approach avoids misoperation and data loss, allowing for more accurate debugging and handling of query errors.

Hopefully this article can help you better understand the use of mysqli::$errno and provide a solution that allows you to correctly obtain the error code for each query.