Current Location: Home> Latest Articles> mysqli::$errno and trigger_error() implement error interrupt mechanism

mysqli::$errno and trigger_error() implement error interrupt mechanism

M66 2025-05-28

Program error handling is crucial when developing PHP applications, especially when it comes to operations involving database interactions. The error handling mechanism can not only help us debug and optimize code, but also improve the stability and user experience of the program. PHP provides many ways to deal with errors, where mysqli::$errno and trigger_error() are very useful tools.

In this article, we will discuss how to use mysqli::$errno and trigger_error() to implement a PHP error interrupt mechanism, ensuring that the program can be terminated in time and given clear error information when encountering database or other serious errors.

1. What are mysqli::$errno and trigger_error() ?

  • mysqli::$errno : This is a property of the mysqli class that saves the error code for the latest database operation. When we perform database operations, if an error occurs, we can obtain detailed error code through mysqli::$errno to help us determine the error type.

  • trigger_error() : This is a PHP built-in function to trigger user-level errors. It allows us to throw error messages in specific situations and control the level of errors (for example: warning, notification, or fatal error).

2. Necessity of Error Interrupt Mechanism

In the absence of a good error handling mechanism, PHP programs may continue to execute when database errors or other exceptions are encountered, resulting in unpredictable behavior and even causing more serious errors. For example, when executing a database query, if an error occurs (such as failure to connect to the database or the query fails) and the program fails to interrupt in time, subsequent code may continue to be executed, resulting in data corruption or inconsistency.

Therefore, using mysqli::$errno and trigger_error() can effectively prevent such problems and terminate program execution when an error occurs to ensure the stability of the code.

3. Implement PHP error interrupt mechanism

Let's see how to implement a simple error interrupt mechanism using mysqli::$errno and trigger_error() .

3.1 Database connection and error handling

First, we need to establish a database connection and check whether it is successful. If the connection fails, we can use trigger_error() to trigger a fatal error and abort the program execution.

 <?php
// Database connection configuration
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'test';

// Create a database connection
$mysqli = new mysqli($host, $username, $password, $dbname);

// Check if the connection is successful
if ($mysqli->connect_errno) {
    // Connection failed,Trigger a fatal error and interrupt the program
    trigger_error("数据库Connection failed: " . $mysqli->connect_error, E_USER_ERROR);
}
?>

In the above code, if the database connection fails, $mysqli->connect_errno will return a non-zero error code, and trigger_error() will be triggered, output the error message and terminate the program execution.

3.2 Execute SQL query and check for errors

In addition to error checking when connecting to the database, similar error handling is required when executing SQL queries. We can get the error code of the query through mysqli::$errno . If the query fails, we can use trigger_error() to interrupt the program and report the error.

 <?php
// Execute a query
$query = "SELECT * FROM users";
$result = $mysqli->query($query);

// Check whether the query is successful
if ($mysqli->errno) {
    // Query failed,Trigger an error and interrupt the program
    trigger_error("SQL Query failed: " . $mysqli->error, E_USER_ERROR);
}
?>

In this example, if the SQL query fails to execute, $mysqli->errno will return an error code, trigger_error() will be called, and the error message will be output. This ensures that if there is a problem with the query, the program will be terminated in time and avoid further execution of invalid code.

3.3 Use trigger_error() to set the error level

trigger_error() allows us to set the error level. For example, we can choose to handle some errors as warnings ( E_USER_WARNING ) and some critical errors (such as database connection failure) as fatal errors ( E_USER_ERROR ). In this way, developers can flexibly control how to handle errors.

 <?php
// Suppose a query returns an empty result set
if ($result->num_rows === 0) {
    // Trigger warning,But the procedure does not terminate
    trigger_error("warn: Query result is empty", E_USER_WARNING);
} else {
    // Process query results
    while ($row = $result->fetch_assoc()) {
        echo $row['username'];
    }
}
?>

In this example, if the query returns an empty result set, we trigger a warning via trigger_error() , but the program does not interrupt, but continues to execute. This approach is suitable for situations where the procedure does not need to be terminated immediately.

4. Summary

By utilizing mysqli::$errno and trigger_error() , we can build an effective error interrupt mechanism to ensure that PHP applications can be terminated in time when encountering serious errors, prevent errors from spreading, and improve code stability. By flexibly setting different error levels with trigger_error() , we can also control how errors are handled to ensure that the program can provide clear error information when problems arise.

In actual development, good error handling can not only help developers quickly locate problems, but also ensure that the system shows expected behavior when abnormalities occur, improving user experience and code robustness.