Current Location: Home> Latest Articles> Forgot to release the result set causes $errno to change abnormally

Forgot to release the result set causes $errno to change abnormally

M66 2025-05-29

When using mysqli for database operations, we often encounter the problem of abnormal changes in mysqli::$errno . mysqli::$errno is an attribute used to obtain error codes during database connection or querying. It can usually help developers troubleshoot errors. However, when we make a query, we occasionally find that the value of mysqli::$errno has changed inconsistent with expectations, resulting in unexpected behavior in the program. At this time, a potential reason is often overlooked - the result set is not released.

What is mysqli::$errno ?

mysqli::$errno is a property of the mysqli object. It stores the error code generated during the previous MySQL operation (such as query, connection, etc.). If the operation is successful, mysqli::$errno will return 0 ; if the operation fails, it will return a non-zero error code. You can find the specific cause of the MySQL error based on these error codes.

Common error codes include:

  • 1045: Access denied (Access denied for user)

  • 1064: Syntax error (Syntax error)

  • 2002: Can't connect to MySQL server (can't connect to MySQL server)

Usually, when the database operation fails, we can use mysqli::$errno and mysqli::$error (Error message) to determine the problem.

Why does it change abnormally?

When using mysqli for querying, the value of mysqli::$errno sometimes becomes unmeaning as expected, especially after the query is executed. If you do not release the query result set in time, mysqli::$errno may become an unreal error code. This is because MySQL's connection resources are not fully released, resulting in subsequent queries that may be affected.

The role of releasing the result set

When we use functions such as mysqli_query() to execute a query, MySQL will save the query results in memory. In order to save resources, after the query is completed, we should use the mysqli_free_result() function to explicitly release the query result set. This not only helps free memory, but also avoids subsequent operations that may be affected by unreleased result sets.

No result set is released

Suppose you execute a query and after the query is completed, mysqli_free_result() is not called to free the result set. Next, when you try to perform another query operation, you may encounter the following situation:

 <?php
// Connect to the database
$mysqli = new mysqli('localhost', 'root', 'password', 'test_db');

// Execute the first query
$result1 = $mysqli->query("SELECT * FROM users");

// If no result set is released
// $mysqli->free_result($result1);  // Ignore the release

// Execute the second query
$result2 = $mysqli->query("SELECT * FROM orders");

// Get error message
echo "Error code: " . $mysqli->errno;
?>

In the above code, since we did not release the result set of the first query, $mysqli->errno may have an exception in the second query, resulting in the inability to obtain the error code correctly, and even a error status code is returned. This is the root cause of the abnormal change of mysqli::$errno .

How to solve it?

In order to avoid abnormal changes in mysqli::$errno , we must ensure that the query result set is released in time after each query is executed. The correct way to do it is:

 <?php
// Connect to the database
$mysqli = new mysqli('localhost', 'root', 'password', 'test_db');

// Execute the first query
$result1 = $mysqli->query("SELECT * FROM users");

// Release the result set
if ($result1) {
    $result1->free();
}

// Execute the second query
$result2 = $mysqli->query("SELECT * FROM orders");

// Get error message
echo "Error code: " . $mysqli->errno;
?>

By explicitly releasing the result set, it is possible to ensure that the resources after each query are cleaned up and avoid affecting subsequent operations.

summary

When using mysqli for database operations, the reason for abnormal changes in mysqli::$errno is often because of forgetting to release the query result set. Releasing the result set not only helps save memory, but also ensures that subsequent operations are not affected. Therefore, after each query is completed, we should use mysqli_free_result() or $result->free() in time to release the query result set, thereby avoiding unnecessary errors and exceptions.

Related resources

To help you better understand mysqli operations and how to avoid errors, you can refer to the following resources: