Current Location: Home> Latest Articles> Use $errno to assist logging database operation errors

Use $errno to assist logging database operation errors

M66 2025-05-29

How to use mysqli::$errno to assist in logging error messages in database operations?

In PHP, the mysqli extension provides us with a powerful database operation interface. Whether it is a query, insert, update or delete operation to the database, you may encounter some errors. In order to help developers discover problems and debug in a timely manner, mysqli provides some methods and properties, among which mysqli::$errno is an attribute used to obtain error numbers, which can be used to assist us in recording error information in database operations.

This article will introduce how to use mysqli::$errno to record error information and perform effective error tracking through log files.

1. What is mysqli::$errno ?

mysqli::$errno is a property of the mysqli class that is used to get the error code for the last MySQL operation. If no error occurs, the value of mysqli::$errno will be 0 .

For example, when executing a database query, if there is an error in the query statement, mysqli::$errno will return an error number, which can help us understand what is going on. Also used with mysqli::$errno is mysqli::$error , which returns a specific description of the error message.

2. How to use mysqli::$errno ?

In order to record the error information in the database operation, first, we need to perform a database operation and determine whether an error has occurred. If an error occurs, we can get the error number and information through mysqli::$errno and mysqli::$error , and then log these error information to the log.

Here is a simple example:

 <?php
// Connect to the database
$mysqli = new mysqli('localhost', 'user', 'password', 'database');

// Check database connections
if ($mysqli->connect_error) {
    die('Connection failed: ' . $mysqli->connect_error);
}

// Perform a query operation
$sql = "SELECT * FROM non_existent_table"; // Written an incorrect one on purpose SQL Statement
$result = $mysqli->query($sql);

// Check for errors
if ($mysqli->errno) {
    // Log error message to log file
    error_log("Database query error - Error number: " . $mysqli->errno . " error message: " . $mysqli->error, 3, '/path/to/your/logfile.log');
    echo "An error occurred,Logged in log。";
} else {
    // Process query results
    echo "Query successful!";
}

// Close the connection
$mysqli->close();
?>

3. Error logging to log

In the above code example, we used the error_log() function to record the error message. The parameters of this function include:

  • The first parameter is an error message, usually a string that includes the error number and specific error message.

  • The second parameter is the type of the log, here we use 3 to specify that the error information is written to the file.

  • The third parameter is the path of the log file, here you need to replace it with your actual log file path.

In this way, once an error occurs, relevant error information (including mysqli::$errno and mysqli::$error ) will be recorded in the specified log file for easier subsequent viewing and troubleshooting.

4. Why use mysqli::$errno to record errors?

The benefits of using mysqli::$errno to record errors are as follows:

  • Quick positioning problem : Through error numbers, you can quickly understand which specific operation is wrong, avoiding vague error prompts.

  • System monitoring : Through logging, you can monitor the health status of database operations and promptly discover potential database problems.

  • Easy to debug : During the development stage, by recording detailed error information, developers can debug code more effectively.

5. Summary

mysqli::$errno is a very useful tool that helps developers catch errors and log in database operations. By logging the error number and details into the log file, we can quickly spot the problem and take appropriate measures to fix it. I hope that the sample code and explanation of this article can help you better understand the usage of mysqli::$errno , thereby improving the robustness of database operations.