W3docs

error

In this article, we will focus on the mysqli_error() function in PHP, which is used to get the error message associated with the most recent MySQLi operation.

In this article, we will focus on the mysqli_error() function in PHP, which is used to retrieve the error message from the last MySQLi function call. This page covers the function's syntax, parameters, and return value, walks through practical examples, and points to related error-handling functions.

Introduction to the mysqli_error() function

The mysqli_error() function is a built-in PHP function that returns a string describing the error from the most recent MySQLi operation performed on a given connection. If the last call succeeded — or no operation has run yet — it returns an empty string ("").

You need it because most MySQLi functions (such as mysqli_query()) signal failure only by returning false. That tells you something went wrong, but not what. mysqli_error() fills the gap by giving you the human-readable message reported by the MySQL server, for example Table 'database.my_table' doesn't exist.

Syntax

mysqli_error(mysqli $mysqli): string

Parameter

  • $mysqli (required) — A MySQLi connection object returned by mysqli_connect() (or mysqli_init() + mysqli_real_connect()). The error reported belongs to this specific connection.

Return value

A string with the error description for the last operation on $mysqli. An empty string means there was no error. Because an empty string is "falsy" in PHP, you typically branch on the result of the failing call (such as !$result) rather than on mysqli_error() itself.

Object vs. procedural style. This page uses the procedural style, mysqli_error($mysqli). With the object-oriented API the equivalent is the read-only property $mysqli->error.

How to use the mysqli_error() function

Using the mysqli_error() function is straightforward. You call it on a valid MySQLi connection object. Here is an example:

How to use the mysqli_error() function?

<?php
$mysqli = mysqli_connect("localhost", "username", "password", "database");

$query = "SELECT * FROM my_table";
$result = mysqli_query($mysqli, $query);

if (!$result) {
    $error_msg = mysqli_error($mysqli);
    echo "Failed to execute query. Error message: " . $error_msg;
    exit();
}

mysqli_close($mysqli);
?>

In this example, we call the mysqli_connect() function to connect to a MySQL database. We then execute a query using the mysqli_query() function and store the result in a variable. We check if there was an error in the query using the !$result condition. If there was an error, we pass the connection object to mysqli_error() to retrieve the specific error message, display it, and terminate the script.

Note: Always verify that mysqli_connect() returns a valid object before running queries. If the connection fails, mysqli_query() will trigger a PHP warning.

Advanced usage

The mysqli_error() function can also be used across multiple connections. Here is an example:

Advanced usage of PHP mysqli_error()

<?php
$mysqli1 = mysqli_connect("localhost", "username", "password", "database1");
$mysqli2 = mysqli_connect("localhost", "username", "password", "database2");

$query = "SELECT * FROM my_table";
$result1 = mysqli_query($mysqli1, $query);
$result2 = mysqli_query($mysqli2, $query);

if (!$result1) {
    $error_msg = mysqli_error($mysqli1);
    echo "Failed to execute query on connection 1. Error message: " . $error_msg;
    exit();
}

if (!$result2) {
    $error_msg = mysqli_error($mysqli2);
    echo "Failed to execute query on connection 2. Error message: " . $error_msg;
    exit();
}

mysqli_close($mysqli1);
mysqli_close($mysqli2);
?>

In this example, we establish two separate connections and run the same query on each. We check the result for each connection independently. If a query fails, we pass the corresponding connection object to mysqli_error() to isolate and display the specific error. This matters because mysqli_error() is connection-scoped — calling it on $mysqli1 never reports an error that happened on $mysqli2.

Modern error handling with exceptions

For newer PHP applications, consider enabling exception-based error handling instead of checking mysqli_error() after every call. Add this line before you connect:

<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

try {
    $mysqli = mysqli_connect("localhost", "username", "password", "database");
    $result = mysqli_query($mysqli, "SELECT * FROM my_table");
} catch (mysqli_sql_exception $e) {
    echo "Database error: " . $e->getMessage();
}
?>

With MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT set, a failed connection or query automatically throws a mysqli_sql_exception object instead of returning false. The same message you would have read from mysqli_error() is available through $e->getMessage(). As of PHP 8.1 this reporting mode is the default, so most modern code relies on try/catch rather than manual checks.

Common gotchas

  • Passing the wrong connection. mysqli_error() reports errors for the connection you pass in. Pass the same $mysqli you used for the failing query.
  • Calling it after mysqli_close(). Once a connection is closed, the object is no longer valid and you cannot read its error.
  • Connection failures. If mysqli_connect() itself fails it returns false (or throws, in strict mode). Use mysqli_connect_error() for connection-time errors — mysqli_error() is for errors after a successful connect.

Conclusion

In conclusion, the mysqli_error() function is a practical tool for debugging and handling MySQLi errors. By verifying connection validity and understanding how to retrieve specific error messages, you can build more resilient database interactions.

Practice

Practice
What are some ways to handle errors in PHP?
What are some ways to handle errors in PHP?
Was this page helpful?