How to display errors for my MySQLi query with PHP?

Spread the love

Sometimes, we want to display errors for my MySQLi query with PHP.

In this article, we’ll look at how to display errors for my MySQLi query with PHP.

How to display errors for my MySQLi query with PHP?

To display errors for my MySQLi query with PHP, we use the mysqli_error function.

For instance, we write

$sql = "...";
($result = mysqli_query($conn, $sql)) or
    trigger_error(
        "Query Failed! SQL: $sql - Error: " . mysqli_error($conn),
        E_USER_ERROR
    );

to call mysqli_query to make the $sql query.

We add

or
    trigger_error(
        "Query Failed! SQL: $sql - Error: " . mysqli_error($conn),
        E_USER_ERROR
    );

to trigger an error if there’s an error.

And we call mysqli_error with $conn to get the error.

Conclusion

To display errors for my MySQLi query with PHP, we use the mysqli_error function.

Leave a Reply

Your email address will not be published.