Current Location: Home> Latest Articles> How to map all MySQL error codes and their meanings through $errno

How to map all MySQL error codes and their meanings through $errno

M66 2025-05-17

In development, we may encounter various errors when using mysqli to connect to a MySQL database. To debug these problems, we can use mysqli::$errno to quickly obtain error information and understand the specific cause of the error. This article will introduce how to use mysqli::$errno and the common MySQL error code meanings.

1. What is mysqli::$errno ?

mysqli::$errno is a property in the mysqli class that stores the error code for the last MySQL operation. We can check whether the database operation is successful by accessing this property. If the operation fails, $errno will return an error code, and we can diagnose the problem based on this error code.

2. How to get MySQL error using mysqli::$errno ?

First, we need to connect to the MySQL database and execute a SQL statement. If an error occurs while executing the SQL statement, mysqli::$errno will be set to the corresponding error code. Here is an example:

 <?php
// create mysqli Object and connect to the database
$mysqli = new mysqli("localhost", "username", "password", "database_name");

// Check if the connection is successful
if ($mysqli->connect_errno) {
    echo "Failed to connect to the database: " . $mysqli->connect_error;
    exit();
}

// implement SQL Statement
$result = $mysqli->query("SELECT * FROM non_existent_table");

// Check whether the query is successful
if (!$result) {
    echo "Query failed,Error code: " . $mysqli->errno . "<br>";
    echo "error message: " . $mysqli->error;
}

$mysqli->close();
?>

In the above code, we deliberately executed a query for a table that does not exist ( non_existent_table ), which triggers an error. $mysqli->errno will return an error code, and we can find out the specific cause of the error based on the error code.

3. Common MySQL error code meanings

The following are some common MySQL error codes and their meanings to help developers better understand error messages:

Error code error message explain
1045 Access denied for user User authentication failed, usually with user name or password error
1049 Unknown database The database does not exist
1146 Table 'xxx' doesn't exist The table does not exist
1064 You have an error in your SQL syntax SQL syntax error
1062 Duplicate entry for key 'PRIMARY' Primary key conflict, duplicate primary key inserted
2002 Can't connect to local MySQL server Unable to connect to MySQL server, usually the server is not started
2013 Lost connection to MySQL server Lost connection, possible network problem or server timeout
1142 SELECT command denied The user does not have permission to perform SELECT operations
1366 Incorrect integer value The inserted data type does not match the field type
1054 Unknown column The query column does not exist

4. How to deal with problems based on error codes?

When you encounter the error code provided by mysqli::$errno , here are some common methods of handling it:

  • Error code 1045 (Access denied for user) : This means that the provided username or password is incorrect. Make sure you connect with the correct credentials and check the database login information saved in the configuration file.

  • Error code 1049 (Unknown database) : This error indicates that the connected database does not exist. You need to make sure the database name is spelled correctly, or create the corresponding database on the database server.

  • Error code 1064 (You have an error in your SQL syntax) : This is a SQL syntax error. Check whether the SQL query statement has any typos and make sure that the SQL statement complies with the syntax rules.

  • Error code 1062 (Duplicate entry for key 'PRIMARY') : This error indicates that you are trying to insert a duplicate primary key value. You can check the inserted data to make sure there are no duplicates, or change the primary key value.

  • Error code 1146 (Table 'xxx' doesn't exist) : This error indicates that the query table does not exist. You need to make sure the table has been created, or make sure the table name in the query statement is correct.

5. Summary

mysqli::$errno is a very practical tool that can help you quickly locate the causes of MySQL errors. By combining error codes and error messages, you can solve problems more efficiently. In addition, understanding common MySQL error codes and their meanings can also help improve debugging efficiency during development.

Hopefully this article can help you better understand and use mysqli::$errno and more easily deal with MySQL errors. If you encounter other problems, you can also refer to the official MySQL error code document for further search.