Current Location: Home> Latest Articles> How to Use mysqli::$errno in Custom Error Handlers to Handle MySQL Errors?

How to Use mysqli::$errno in Custom Error Handlers to Handle MySQL Errors?

M66 2025-07-18

In PHP, we often need to interact with databases, especially when using MySQL databases. The mysqli extension is a very common choice. However, when we encounter database connection errors or query errors, it's essential to have an appropriate way to handle these issues. mysqli::$errno is an important property in the mysqli class that returns the error code associated with the most recent MySQL error. This article will explain how to use mysqli::$errno in a custom error handler to handle MySQL errors.

1. Custom Error Handlers

PHP provides the set_error_handler() function to set a custom error handler. We can use this function to capture and handle errors. When using mysqli to connect and query a MySQL database, if an error occurs, mysqli::$errno can help us identify the error type and allow us to take specific actions for error handling.

2. Using mysqli::$errno to Capture MySQL Errors

mysqli::$errno returns an integer value representing the error code of the most recent MySQL operation. If no error occurred, it will return 0. If it returns any other value, an error has occurred. By combining this with a custom error handler, we can categorize errors for specific actions like logging, displaying error messages, or retrying the operation.

3. Example Code

Here’s an example using mysqli::$errno to handle MySQL errors. We'll set up a custom error handler and use mysqli::$errno in the database connection and query to check for errors.

<?php
// Set up custom error handler
set_error_handler("customErrorHandler");
<p>function customErrorHandler($errno, $errstr, $errfile, $errline) {<br>
// Check MySQL error code<br>
if ($errno == E_USER_WARNING) {<br>
echo "MySQL Error [$errno]: $errstr\n";<br>
} else {<br>
echo "Error [$errno]: $errstr in $errfile on line $errline\n";<br>
}<br>
}</p>
<p>// Create database connection<br>
$mysqli = new mysqli("localhost", "username", "password", "database_name");</p>
<p>// Check connection<br>
if ($mysqli->connect_errno) {<br>
trigger_error("Failed to connect to MySQL: " . $mysqli->connect_error, E_USER_WARNING);<br>
exit();<br>
}</p>
<p>// Execute query<br>
$query = "SELECT * FROM non_existent_table";<br>
$result = $mysqli->query($query);</p>
<p>// Check query execution<br>
if (!$result) {<br>
trigger_error("MySQL Query Error: " . $mysqli->error, E_USER_WARNING);<br>
}</p>
<p>// Close connection<br>
$mysqli->close();<br>
?><br>

4. Code Explanation

  • In the above code, we first use the set_error_handler() function to set a custom error handler named customErrorHandler.

  • If the MySQL connection fails (e.g., the database server is unavailable), we get the connection error code using mysqli->connect_errno, and pass the error message to the custom error handler using trigger_error().

  • Similarly, if the query execution fails, we retrieve the error message using mysqli->error and trigger a warning (E_USER_WARNING), which is captured by the custom error handler.

  • The custom error handler will print the error code and message, helping developers locate the issue.

5. Handling MySQL Error Codes

mysqli::$errno not only returns the error code but can also be used with MySQL's error code documentation to help pinpoint specific issues. For example, if the error code is 1064, it indicates a SQL syntax error; if the error code is 1045, it means there was a database authentication failure.

6. Replacing URL Domain

When you need to handle URL-related errors in your code, you can use mysqli::$errno to capture and replace the domain name in the URL. For example, if you find an error related to database operations and want to replace the domain name with m66.net, you can modify the URL as follows: