Current Location: Home> Latest Articles> Does mysqli::$errno Returning 0 means everything is normal?

Does mysqli::$errno Returning 0 means everything is normal?

M66 2025-05-29

When using PHP for database operations, we usually interact with the MySQL database through the mysqli extension. MySQL provides rich features, one of the commonly used attributes is errno , which represents the error code for database operations. If mysqli::$errno returns 0, it usually means no error has occurred. However, does returning 0 mean that there is no problem with the database operation? This article will discuss this issue in detail.

1. Introduction to mysqli::$errno

In PHP, mysqli::$errno is a property of the mysqli object, returning the error code of the last database operation. Specifically:

  • If the operation is successful and no errors occur, the value of errno will be 0.

  • If the operation fails, errno returns a non-zero error code, and you can use the mysqli::$error attribute to get more detailed error information.

Code example:

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

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

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

if ($mysqli->errno) {
    echo "Query error: " . $mysqli->error;
} else {
    echo "Query successful!";
}
?>

In this example, if the query is successful, $mysqli->errno returns 0. If the query fails, $mysqli->errno returns a non-zero error code, and $mysqli->error will provide specific information about the error.

2. errno is 0 does not mean there is no problem

While mysqli::$errno is 0 usually means that there are no errors in the database operation, it does not mean that the operation is completely fine. Here are some common situations where the problem may still exist even if errno returns 0.

2.1 The database connection is successful, but there is a logical problem with the query statement

Even if the database connection is successful, the query SQL statement may be logically wrong. For example, the query table does not exist or the returned result is empty.

Example:

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

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

$query = "SELECT * FROM non_existent_table";  // Query a table that does not exist
$result = $mysqli->query($query);

if ($mysqli->errno) {
    echo "Query error: " . $mysqli->error;
} else {
    if ($result->num_rows == 0) {
        echo "No data found!";
    } else {
        echo "Query successful!";
    }
}
?>

In this example, although mysqli::$errno returns 0, the query table does not exist, so no data is actually found. Although there is no error code, the logic problem still exists.

2.2 Warning information will not affect errno

MySQL extension generates some warning messages, but these warnings are not captured by errno . That is, even if there are some warnings, errno may still be 0.

For example, when certain queries are executed, MySQL may issue a warning instead of an error. If it's just a warning, errno is still 0, but you should be careful if MySQL returns a warning.

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

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

$query = "SELECT id FROM users LIMIT 10";  // Assumptions users Table has id Fields,But some data are missing
$result = $mysqli->query($query);

if ($mysqli->errno) {
    echo "Query error: " . $mysqli->error;
} else {
    echo "Query successful!";
    // If there is a warning,Can be passed `mysqli::$warnings` Check
    $warnings = $mysqli->warnings;
    if ($warnings) {
        echo "Warning message: " . $warnings->message;
    }
}
?>

3. How to ensure that there are no problems with the database operation?

Although mysqli::$errno returns 0 usually means no errors, to ensure the integrity of the database operation, we can take the following measures:

3.1 Check the query results

Even if there is no error code, check whether the query returns the expected result. For example, you can check mysqli::num_rows to verify that the SELECT query returns data.

3.2 Using mysqli::$warnings

By checking the mysqli::$warnings property, you can capture potential warning messages, making sure no potential issues are missing.

3.3 Use try-catch exception handling

While mysqli does not throw exceptions by itself, you can make it throw exceptions by setting mysqli_report(MYSQLI_REPORT_STRICT) to handle errors better.

Example:

 <?php
mysqli_report(MYSQLI_REPORT_STRICT);

try {
    $mysqli = new mysqli("localhost", "user", "password", "database");
    $query = "SELECT * FROM users";
    $result = $mysqli->query($query);
    
    // Process query results
} catch (mysqli_sql_exception $e) {
    echo "Catched exception: " . $e->getMessage();
}
?>

In this way, you can catch exceptions and process them accordingly when the database operation fails.

in conclusion

mysqli::$errno returns 0 indicating that there is no error at the database operation level, but does not mean that the database operation is completely free of problems. In order to ensure the correctness of database operations, developers should conduct additional checks, including the validity of query results, the processing of warning information, and the use of exception handling. Only by considering these factors in a comprehensive way can the reliability and robustness of database operations be ensured.