Current Location: Home> Latest Articles> Why does mysqli::$errno return 0 when a transaction fails? What other functions should be used to diagnose the issue?

Why does mysqli::$errno return 0 when a transaction fails? What other functions should be used to diagnose the issue?

M66 2025-06-23

In PHP database operations, mysqli is one of the most commonly used methods for connecting to and manipulating MySQL databases. In many cases, we use transactions to ensure the atomicity of database operations. However, sometimes when diagnosing errors with mysqli::$errno, you may encounter a confusing situation where mysqli::$errno returns 0 even when a transaction fails, making it difficult to diagnose the problem.

Transactions and Error Codes

First, let's understand how mysqli::$errno works. mysqli::$errno returns the error code of the last database operation. When the operation is successful, its value is 0; when it fails, it returns a non-zero error code. However, the problem arises because some database operations (especially those involving transactions) do not always directly update mysqli::$errno, even if the operation fails, which may result in no significant error code being set.

Transaction Failure Yet 0 is Returned

If you encounter issues with a transaction and find that mysqli::$errno is still 0 even though the transaction fails, it does not mean no error occurred. In fact, the transaction operation itself might not have triggered an error code directly. For example, when using mysqli::begin_transaction() or mysqli::commit(), if no serious errors are detected by the database, it might not return an error code but simply mark the transaction as "failed".

This phenomenon typically occurs in the following situations:

  1. Autocommit Mode: If mysqli::$auto_commit is set to true within a transaction, the transaction might be automatically committed, even when errors occur, without explicit rollback.

  2. SQL Errors Not Apparent: Some errors (like data constraint violations) might not immediately appear in mysqli::$errno, especially if database errors are suppressed.

Using Other Functions to Diagnose the Problem

If mysqli::$errno returns 0 and you suspect a transaction operation failed, you can combine the following functions to diagnose the issue:

  1. mysqli::$error:
    mysqli::$error returns the error message for the database operation. When mysqli::$errno is 0, checking mysqli::$error can help you obtain more detailed error information. Even without an explicit error code, the database error message may still be available as a string.

    if ($mysqli->errno) {  
        echo "Error: " . $mysqli->error;  
    }  
    
  2. mysqli::error_list:
    For MySQL 5.5+ versions, mysqli::error_list provides an error list that allows you to retrieve more detailed error information, including multiple error causes, which is especially useful when dealing with complex transactions.

    $errors = $mysqli->error_list;  
    foreach ($errors as $error) {  
        echo "Error code: " . $error['errno'] . " - " . $error['error'];  
    }  
    
  3. mysqli::rollback():
    If you encounter transaction failure issues, you can explicitly call rollback() when the transaction fails to roll it back and use the error information to analyze the issue. This helps avoid data inconsistencies caused by incomplete transactions.

    $mysqli->rollback();  
    echo "Transaction failed, rolled back.";  
    
  4. SHOW ENGINE INNODB STATUS:
    This is a built-in MySQL command used to retrieve InnoDB engine status information. By querying this command, you can view detailed transaction-related information, including deadlocks, lock waits, and more. You can execute this command with mysqli::query() to gather more clues.

    $result = $mysqli->query("SHOW ENGINE INNODB STATUS");  
    $status = $result->fetch_assoc();  
    echo $status['Status'];  
    
  5. Check Database Logs:
    In some cases, the database error logs may provide more useful information, especially when low-level transaction errors cannot be detected through PHP's error codes. You can check MySQL's error log files or use tools like phpMyAdmin to view these logs.

Conclusion

When using mysqli::$errno, if a transaction fails and 0 is returned, it could be due to several reasons, such as autocommit mode, SQL errors not being immediately visible, and others. To more accurately diagnose the transaction failure issue, we can combine functions like mysqli::$error, mysqli::error_list, SHOW ENGINE INNODB STATUS, and other methods to further analyze the root cause of the problem. Always handle transaction begin, commit, and rollback operations carefully to ensure data consistency.