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. We will provide you with an overview of the function, how it works, and examples of its use.

Introduction to the mysqli_error() function

The mysqli_error() function is a built-in PHP function that returns the error message from the last MySQLi operation. If no error has occurred, it returns an empty string. This function is useful when you need to handle errors in your MySQLi code and take appropriate action based on the returned message.

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.

Note on modern error handling: For newer PHP applications, consider enabling exception-based error handling with mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT). This automatically throws mysqli_sql_exception objects on errors, reducing the need for manual mysqli_error() checks in many cases.

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?