Current Location: Home> Latest Articles> What is the difference between mysqli::$errno and mysqli::$error? Which one is more suitable for catching error messages?

What is the difference between mysqli::$errno and mysqli::$error? Which one is more suitable for catching error messages?

M66 2025-05-18

In PHP, the mysqli extension is a common tool for handling MySQL database connections and queries. Unlike mysql extensions, mysqli provides more functionality and flexibility and supports object-oriented programming. When using mysqli for database operations, we may need to capture error messages to debug the program or display useful prompts to the user. PHP provides two properties : mysqli::$errno and mysqli::$error to help developers get error information, but they have some subtle differences and are suitable for different scenarios. This article will analyze their differences in depth and discuss which attribute is more suitable for catching error information.

1. Definitions of mysqli::$errno and mysqli::$error

  • mysqli::$errno : Returns the error code (integer form) that occurred during the last database operation. If no error occurs, return 0 . This error code is a number defined by a MySQL database and is used to represent different types of errors.

  • mysqli::$error : Returns the error message (in string form) that occurred during the last database operation. If no error occurs, an empty string is returned. This error message is usually a detailed description returned by MySQL to help developers understand what is going on.

2. The main differences between mysqli::$errno and mysqli::$error

  • Error Type :
    mysqli::$errno returns an error code (integral), while mysqli::$error returns a string (text) describing the error. This means that mysqli::$errno is suitable for error classification and judgment in programs (such as different processing based on error code), while mysqli::$error is more suitable for providing detailed error information for easy debugging.

  • Error content :
    Error codes are defined internally in MySQL, and each error code corresponds to a specific type of error. For example, 1062 means that a unique constraint is violated when inserting data. The error message is more intuitive, such as "Duplicate entry for key 'PRIMARY'" describes the specific cause of the error. Developers usually check the error code to determine the problem category first, and then debug based on the error message.

  • Performance differences :
    Getting the error code ( mysqli::$errno ) is relatively lighter because it is just a simple integer value, while getting the error message ( mysqli::$error ) requires returning a string, which may affect performance slightly in some cases, especially when performing frequent database operations.

3. Which one is more suitable for catching error messages?

  • Using mysqli::$errno :
    If you only care about whether an error has occurred and need to do specific processing based on the error code (for example, when the error code is 1045 , it means that the database connection permission is insufficient), then mysqli::$errno will be more appropriate. It provides a simple, clear way to judge error categories.

  • Using mysqli::$error :
    If you need to get more detailed error information, or you are developing a tool for debugging, mysqli::$error is the better choice. The error message provides a specific problem description returned by MySQL, which helps you locate the problem faster.

4. Sample code

Here is a simple example that demonstrates how to use mysqli::$errno and mysqli::$error to capture error messages:

 <?php
// Create a database connection
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check if the connection is successful
if ($mysqli->connect_errno) {
    echo "Connection failed: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    exit();
}

// Execute a query
$result = $mysqli->query("SELECT * FROM non_existing_table");

if (!$result) {
    // use mysqli::$errno Get the error code
    echo "Error code: " . $mysqli->errno . "\n";
    
    // use mysqli::$error Get error message
    echo "error message: " . $mysqli->error . "\n";
}

$mysqli->close();
?>

In this example, if the query execution fails, we can get the error code through mysqli::$errno and get the detailed error information through mysqli::$error .

5. Summary

  • mysqli::$errno returns an integer type error code suitable for error classification and processing.

  • mysqli::$error returns a string type error message, suitable for debugging and displaying specific error descriptions.

For catching error information, if you need to do different processing based on the error code, using mysqli::$errno is a better choice. And if you need to debug and view the specific error description, it would be more appropriate to use mysqli::$error .