error_list
In this article, we will focus on the mysqli_error_list() function in PHP, which is used to get an array of errors that occurred during the most recent MySQLi
The mysqli_error_list() function returns an array of the errors raised by the most recent MySQLi function call on a given connection. Unlike mysqli_error(), which gives you only the last error message as a string, mysqli_error_list() returns every error from that operation — each as an associative array containing the error number, the SQLSTATE code, and the human-readable message. This page explains the syntax, the shape of the returned data, and how to use it in practice.
Syntax
mysqli_error_list(mysqli $mysql): arrayObject-oriented style uses the read-only error_list property instead:
$mysqli->error_list;Parameters
$mysql— A MySQLi connection object returned bymysqli_connect()(ormysqli_init()/new mysqli()).
Return value
An array of associative arrays. The array is empty when no error occurred. Each entry has three keys:
| Key | Type | Description |
|---|---|---|
errno | int | The MySQL error number (e.g. 1146 for "table doesn't exist"). |
sqlstate | string | The five-character SQLSTATE error code (e.g. 42S02). |
error | string | The error message text. |
Basic example
You call mysqli_error_list() right after the operation you want to inspect — typically when a query has just failed:
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$error_list = mysqli_error_list($mysqli);
foreach ($error_list as $error) {
echo "Failed to execute query. Error message: " . $error['error'] . "\n";
}
exit();
}
mysqli_close($mysqli);
?>Here we connect, run a query, and check the result. If mysqli_query() returns false, we call mysqli_error_list() and loop over the returned array, printing each error's error message before exiting. When the query succeeds, the loop simply doesn't run because the array is empty.
Reading the error code and SQLSTATE
Because each entry is an associative array, you can read the numeric code and SQLSTATE alongside the message — useful for branching on specific failures rather than parsing message text:
<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");
$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);
if (!$result) {
$error_list = mysqli_error_list($mysqli);
foreach ($error_list as $error) {
echo "Failed to execute query. Error message: " . $error['error'] . "\n";
echo "Error code: " . $error['errno'] . "\n";
echo "SQLSTATE error code: " . $error['sqlstate'] . "\n";
}
exit();
}
mysqli_close($mysqli);
?>This extends the basic example by printing the numeric errno and the sqlstate code for each error, giving you the full diagnostic context.
Object-oriented style
If you create the connection with new mysqli(), use the error_list property:
<?php
$mysqli = new mysqli("localhost", "username", "password", "database");
if (!$mysqli->query("SELECT * FROM missing_table")) {
foreach ($mysqli->error_list as $error) {
echo "[{$error['errno']}] {$error['sqlstate']}: {$error['error']}\n";
}
}
$mysqli->close();
?>mysqli_error_list() vs. related functions
mysqli_error()— returns only the last error message as a string. Use it when you just need a single message.mysqli_errno()— returns the error number of the last call.mysqli_error_list()— returns all errors from the last call, each witherrno,sqlstate, anderror. Use it when a single operation can surface multiple problems.
Gotchas
- The list reflects only the most recent MySQLi call on that connection. Capture it immediately after the failing operation — a subsequent successful call clears it.
- A successful operation returns an empty array, not
falseornull, so guard withif (!$result)(or checkmysqli_errno()) before assuming there are errors to read. - For connection failures, the connect step itself fails before you have a usable object — use
mysqli_connect_error()there instead.
Conclusion
mysqli_error_list() gives you structured access to every error from the last MySQLi operation, complete with error number, SQLSTATE, and message. Reach for it when you need richer diagnostics than mysqli_error() provides. For broader PHP error handling, see PHP error handling and the mysqli overview.