Current Location: Home> Latest Articles> mysqli_stmt::$error Limitation in Multi-Sentence Execution

mysqli_stmt::$error Limitation in Multi-Sentence Execution

gitbox 2025-05-26

In PHP, the mysqli extension is a very common database access tool that provides object-oriented and process-oriented interfaces. The mysqli_stmt class is used to prepare and execute SQL statements. It also has many properties for handling errors, such as mysqli_stmt::$error .

However, when executing multi-statements, the behavior of mysqli_stmt::$error is different from that of a single statement. In this article, we will explore why mysqli_stmt::$error is restricted in multi-statement execution and how to deal with these restrictions.

What is multi-statement execution?

In MySQL, multi-statements are called multi-statements, which are executed in a single query request. When using MySQLi extension, you can execute multi-statements through the mysqli_multi_query() function. This function allows you to send multiple SQL queries and process them one by one in the same connection.

 $query = "SELECT * FROM users; SELECT * FROM orders;";
if (mysqli_multi_query($connection, $query)) {
    do {
        // Process the results of each query
        if ($result = mysqli_store_result($connection)) {
            while ($row = mysqli_fetch_assoc($result)) {
                // Output query results
            }
            mysqli_free_result($result);
        }
    } while (mysqli_next_result($connection));
}

Mysqli_stmt::$error and the limitations of execution of multiple statements

When using the mysqli_stmt::$error property, it returns the error message when executing the statement. This property usually reflects the error of the last query when a single statement is executed. However, in multi-statement execution, the behavior of mysqli_stmt::$error is different.

In multi-statement execution, the mysqli_multi_query() function will process multiple queries at once, which means that the error information of the query is not directly reflected in mysqli_stmt::$error , but is handled by mysqli_multi_query() or mysqli_next_result() . Therefore, it is impossible to get the error information of all queries directly through mysqli_stmt::$error .

Why is there such a limitation?

This is because multi-statement query is not a single query operation, but a combination of multiple queries. When multiple statements are executed, MySQL processes each query one by one, and errors may even occur between different queries. In this case, mysqli_stmt::$error cannot accurately reflect the error message for each query.

The specific reasons are as follows:

  1. Statement-level error handling: In multi-statement execution, the errors in each SQL statement are captured and processed by mysqli_multi_query() and its related functions (such as mysqli_next_result() ), rather than directly captured by mysqli_stmt .

  2. Separation of errors: During the multi-statement execution process, the execution result of each statement is isolated, so the error message is separated from the execution status of each statement. This means you cannot catch all statement errors with a single mysqli_stmt::$error property.

  3. Transaction consistency: If you use transactions (such as START TRANSACTION and COMMIT ) to execute multiple statements, and an error occurs in one of the queries, the transaction will automatically roll back. However, error capture is still managed by mysqli_multi_query() and transaction mechanisms, not mysqli_stmt .

How to get error information for each query?

Although mysqli_stmt::$error cannot directly provide error information in multi-statement execution, you can still get error information for each query in other ways. A common practice is to use the mysqli_multi_query() function to combine mysqli_next_result() and mysqli_error() .

Here is an example:

 $query = "SELECT * FROM users; SELECT * FROM non_existing_table;";
if (mysqli_multi_query($connection, $query)) {
    do {
        // Check the results of the current query
        if ($result = mysqli_store_result($connection)) {
            while ($row = mysqli_fetch_assoc($result)) {
                // Process query results
            }
            mysqli_free_result($result);
        }

        // Check if there are errors in the current query
        if (mysqli_error($connection)) {
            echo "Query error: " . mysqli_error($connection);
        }
    } while (mysqli_next_result($connection));
}

In this example, after each query is executed, we get the error information for each query through mysqli_error($connection) .

in conclusion

In summary, the limitation of mysqli_stmt::$error in multi-statement execution is mainly because multi-statement query is a combination of multiple independent queries, while mysqli_stmt::$error can only reflect the errors of the current statement. When executing multiple statements, you need to use functions such as mysqli_multi_query() , mysqli_next_result() and mysqli_error() to process the results and errors of the query one by one.

By reasonably handling errors in multi-statement queries, you can more accurately grasp the execution of each query, thereby improving the robustness of the program and debugging efficiency.