Current Location: Home> Latest Articles> Use $errno to catch detailed exceptions in combination with mysqli_sql_exception

Use $errno to catch detailed exceptions in combination with mysqli_sql_exception

M66 2025-05-28

MySQL extension is one of the most common database connection tools when using PHP to manipulate MySQL databases. However, during database operations, some exceptions or errors are inevitable. In order to better catch and handle these exceptions, mysqli_sql_exception and mysqli::$errno provide powerful functions. This article will use examples to illustrate how to capture detailed database exception information in combination with mysqli_sql_exception , and further handle these errors using mysqli::$errno .

1. What is mysqli_sql_exception ?

mysqli_sql_exception is a class in PHP that is specifically used to handle MySQL database exceptions. When executing a database query, if an error occurs, mysqli_sql_exception will throw the corresponding exception. This allows developers to catch and handle these exceptions through try...catch statements without relying on traditional error handling methods (such as mysqli_error or mysqli_errno ).

2. The role of mysqli::$errno

In mysqli_sql_exception , mysqli::$errno records the error code for the last MySQL operation. It is a number that indicates the type of database operation error. For example, mysqli::$errno can help us distinguish whether it is a connection failure, a query syntax error, or a permission problem, etc. Combined with mysqli_sql_exception , error information can be captured and processed in more detail.

3. Basic process of catching and handling exceptions

The basic steps for catching database exceptions using mysqli_sql_exception are as follows:

  1. Turn on exception mode when creating a database connection.

  2. Use the try...catch statement block to catch exceptions in database operations.

  3. Use mysqli::$errno in the catch statement block to get the error code and provide detailed error information or processing logic based on the error code.

4. Sample code

Here is a complete example of using mysqli_sql_exception and mysqli::$errno to catch and handle database exceptions:

 <?php
// Turn on exception handling
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// Create a database connection
try {
    $mysqli = new mysqli("localhost", "root", "password", "test_database");

    // Perform database query operations
    $result = $mysqli->query("SELECT * FROM non_existing_table");

} catch (mysqli_sql_exception $e) {
    // Catch database exceptions
    echo "Database operation failed: " . $e->getMessage() . "<br>";

    // Output error code
    echo "Error code: " . $e->getCode() . "<br>";

    // Further deal with different types of errors
    if ($e->getCode() == 1146) { // 1146 Indicates that the table does not exist
        echo "mistake:The table does not exist,Please check the database structure。";
    } else {
        echo "mistake:Cannot execute query,Please check your SQL grammar。";
    }

    // Can also be used mysqli::$errno Get specific MySQL Error code
    echo "MySQL Error code: " . $mysqli->errno . "<br>";
    echo "MySQL mistake信息: " . $mysqli->error . "<br>";
}
?>

5. Code parsing

  1. Turn on exception report :
    Use the mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); statement to enable the mysqli_sql_exception exception report. This makes mysqli throw a mysqli_sql_exception exception when it encounters an error, rather than reporting an error by returning an error code.

  2. Catch exception :
    Through the try...catch statement, we can catch all mysqli_sql_exception exceptions and process them in the catch statement block.

  3. Get error code and information :
    The error code of the database exception can be obtained through $e->getCode() , and different error prompt information can be provided according to the error code.

  4. Using mysqli::$errno and mysqli::$error :
    Get error code and error information for MySQL databases through $mysqli->errno and $mysqli->error , which is very useful for debugging and handling errors.

6. Optimization of Error Handling

In actual development, in addition to capturing and outputting error information, we can also optimize the error type:

  • Error logging : Record error information into log files for easier subsequent viewing and troubleshooting.

  • User-friendly error prompts : Display friendly prompt information according to the error type to avoid exposing database details to users.

  • Transaction processing : For operations involving multiple queries, transaction mechanisms can be used to ensure data consistency and reliability.

7. Summary

By combining mysqli_sql_exception and mysqli::$errno , we can capture and handle detailed exception information in database operations. This not only helps developers locate problems faster, but also improves the robustness and user experience of the system. Hopefully the examples in this article can help you better understand and use the database exception handling mechanism in PHP.