Current Location: Home> Latest Articles> How to Use mysqli_stmt::$error_list to Retrieve and Manage Multiple Error Messages

How to Use mysqli_stmt::$error_list to Retrieve and Manage Multiple Error Messages

gitbox 2025-08-24

How to Use mysqli_stmt::$error_list to Retrieve and Manage Multiple Error Messages

When using the MySQLi extension for database operations, error handling is a crucial part of the development process. Typically, we use mysqli_error() or mysqli_stmt_error() to get a single error message. However, MySQLi provides the mysqli_stmt::$error_list property, which allows us to capture and manage multiple error messages when executing prepared statements. This is particularly useful for complex SQL operations, helping us better debug and optimize our code.

1. Overview of mysqli_stmt::$error_list

mysqli_stmt::$error_list is an array-like structure that stores all error messages related to the current statement object. It allows us to retrieve a detailed list of errors rather than just a single one. The property returns an array where each element represents a specific error, including the error code and the error text.

2. Using mysqli_stmt::$error_list

To use mysqli_stmt::$error_list, we first need to create a MySQLi connection and then prepare and execute an SQL statement. If multiple errors occur during execution, $error_list will contain all of them. We can then access the error_list property to see the error details.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Create MySQLi connection</span></span><span>
</span><span><span class="hljs-variable">$mysqli</span></span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title function_ invoke__">mysqli</span></span><span>("localhost", "username", "password", "database");
<p></span>// Check connection<br>
if ($mysqli->connect_error) {<br>
die("Connection failed: " . $mysqli->connect_error);<br>
}</p>
<p>// Prepare SQL statement<br>
$stmt = $mysqli->prepare("INSERT INTO users (username, email) VALUES (?, ?)");</p>
<p>// Check if preparation was successful<br>
if ($stmt === false) {<br>
die("SQL Error: " . $mysqli->error);<br>
}</p>
<p>// Bind parameters<br>
$username = "testuser";<br>
$email = "invalidemail.com"; // Assume this email format is invalid<br>
</span>$stmt->bind_param("ss", $username, $email);</p>
<p>// Execute SQL statement<br>
$stmt->execute();</p>
<p>// If errors occur, output the error list<br>
if ($stmt->errno) {<br>
echo "Execution error: " . $stmt->error . "\n";<br>
echo "Error details:\n";</p>
</span><span>print_r($stmt->error_list);

} else {
echo "Insert successful!";
}

// Close statement and connection
$stmt->close();
$mysqli->close();
?>

In this example, we first attempt to insert a record into the users table. Because the email field contains the invalid format invalidemail.com, MySQL returns multiple error messages, which are stored in error_list.

3. Handling Multiple Errors in error_list

The array returned by mysqli_stmt::$error_list contains each error’s components, usually as an associative array with two fields: errno and error. errno represents the error code, and error describes the error message. We can loop through this array to output each error in detail.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Check for multiple errors</span></span><span>
</span><span><span class="hljs-keyword">if</span> (count($stmt->error_list) > 0) {
    </span><span>echo "Multiple errors exist:\n";
    </span><span><span class="hljs-keyword">foreach</span> ($stmt->error_list as $error) {
        </span><span>echo "Error code: " . $error['errno'] . "\n";
        echo "Error message: " . $error['error'] . "\n";
    }
}
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>

This example outputs each error code and corresponding message individually. When multiple errors exist, we can better understand what went wrong and handle each error appropriately.

4. Advantages of mysqli_stmt::$error_list

An important advantage of using mysqli_stmt::$error_list is that it can capture multiple error messages. In complex database operations, errors may occur for several reasons, and error_list allows developers to retrieve all errors at once, improving debugging efficiency. Additionally, this approach enables more precise error handling, such as executing different actions based on error types.

5. Summary

mysqli_stmt::$error_list is a powerful feature provided by MySQLi, allowing developers to capture and manage multiple errors when executing SQL statements. By using this property, we can better understand issues during SQL execution, enhancing database stability and code reliability.