When developing PHP applications, we often interact with the database. When using mysqli extension, mysqli::$errno and mysqli::$error are very common and important tools for catching database errors. However, many developers may misunderstand the purpose of mysqli::$errno and regard it simply as a tool to check for database connection errors. This misunderstanding can make troubleshooting more complicated, especially when errors occur. This article will analyze in detail how to use mysqli::$errno correctly to avoid common misunderstandings.
mysqli::$errno is a property of a mysqli object that is used to store the error code of the previous MySQL operation. If the previous operation was successfully executed, the value of mysqli::$errno will be 0 , otherwise it will return a non-zero error code.
It should be noted that mysqli::$errno is not only suitable for database connection errors, but also for all SQL query operations performed through mysqli .
<?php
// Create a database connection
$mysqli = new mysqli("localhost", "user", "password", "database");
// Check the connection
if ($mysqli->connect_errno) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
// Execute a query
$query = "SELECT * FROM non_existing_table";
$result = $mysqli->query($query);
// Check whether the query is successful
if (!$result) {
echo "Query error: " . $mysqli->error;
echo "Error code: " . $mysqli->errno; // Get the error code
}
$mysqli->close();
?>
In this code, mysqli::$errno will return the error code for the query error (if it exists), and mysqli::$error will provide detailed information about the error.
There are many developers who mistakenly believe that mysqli::$errno is only used to check errors when database connections. If we misunderstand mysqli::$errno , it may lead to missing important error information when troubleshooting other issues. In fact, mysqli::$errno can be used to check for query errors, update errors, and even transaction commit failures, etc.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
// Wrong assumptions:只Check the connectionmistake
if ($mysqli->connect_errno) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
// The wrong way:Don't check other SQL mistake
$query = "SELECT * FROM non_existing_table";
$mysqli->query($query);
// 此处没有Check whether the query is successful,可能错过了Query error
$mysqli->close();
?>
In the above code, the developer may mistakenly believe that he only needs to pay attention to the connection errors and ignores the checking of query errors. Doing so will cause other problems in the database to be unable to catch when the query is executed.
In order to avoid the above misunderstanding, we should check mysqli::$errno after performing each database operation, whether it is a connection operation, query, update, delete and other database operations. Only in this way can potential database problems be discovered earlier.
<?php
// Create a database connection
$mysqli = new mysqli("localhost", "user", "password", "database");
// Check the connectionmistake
if ($mysqli->connect_errno) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
// Execute a query
$query = "SELECT * FROM non_existing_table";
$result = $mysqli->query($query);
// Check whether the query is successful
if (!$result) {
echo "Query error: " . $mysqli->error;
echo "Error code: " . $mysqli->errno; // 获取Query error码
}
$mysqli->close();
?>
In the above code, we not only check for database connection errors, but also check for possible query errors after query. Doing this ensures that we capture the problems in every link, thereby positioning and solving problems more efficiently.
In order to understand the error code returned by mysqli::$errno more clearly, we can consult the MySQL error code document. Here are some common error code examples:
1045 : "Access denied for user" — Username or password error
1146 : "Table doesn't exist" — the table does not exist
1064 : "You have an error in your SQL syntax" — SQL syntax error
Understanding these error codes will help us better interpret error information and quickly locate problems.
mysqli::$errno is mainly used to provide error code, while mysqli::$error returns a detailed error description. When actually troubleshooting problems, you usually need to use these two attributes in combination to fully understand the reasons for the failure of the database operation.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
$query = "SELECT * FROM non_existing_table";
$result = $mysqli->query($query);
if (!$result) {
echo "Query failed: " . $mysqli->error . "<br>";
echo "Error code: " . $mysqli->errno;
}
$mysqli->close();
?>
By outputting error information and error codes at the same time, we can have a clearer understanding of the root cause of the problem and take corresponding measures to fix it.
mysqli::$errno is a very important property in PHP. It is not only used to check database connection errors, but is more widely used in all MySQL operations. In order to avoid troubleshooting caused by misunderstandings, we should check mysqli::$errno after each database operation to ensure that any potential errors can be caught in time. Correct understanding and use of mysqli::$errno can allow us to troubleshoot problems more efficiently during the development process and reduce the occurrence of bugs.