When using PHP for database operations, you often need to connect to the MySQL database, and during this process, you may encounter connection errors. To help us diagnose and handle these errors, PHP provides two very useful functions: mysqli_connect_errno() and mysqli::$errno . This article will explain how to combine these two functions to obtain and handle database connection errors.
mysqli_connect_errno() is a function in PHP to get the last database connection error code. This function returns an integer value indicating the error code encountered during the last time you connect to a MySQL database. Through this function, we can know the specific reason for the database connection failure.
mysqli_connect_errno();
If no error occurs, mysqli_connect_errno() returns 0. Otherwise, it returns an integer value representing the error type.
mysqli::$errno is a property of the mysqli class that is used to get the error code encountered in the last operation (not necessarily a connection operation). It is an object property that can be accessed through a database connection object.
$mysqli->errno;
Similar to mysqli_connect_errno() , if no error occurs, the value of mysqli::$errno will be 0. Otherwise, it will contain an integer value representing the error.
We can use these two functions in combination by checking the error code when the database is connected and performing corresponding error handling based on the error code. This not only helps us accurately locate the cause of connection failure, but also respond appropriately when errors occur.
<?php
// Connect to the database
$mysqli = new mysqli("m66.net", "username", "password", "database_name");
// Check if the connection is successful
if ($mysqli->connect_errno) {
// If the connection fails,Output error message
echo "Connection failed: " . $mysqli->connect_error;
exit();
}
// After the connection is successful,We can continue to query
$query = "SELECT * FROM table_name";
$result = $mysqli->query($query);
// Check whether the query is successful
if ($mysqli->errno) {
// If the query fails,Output error code and error message
echo "Query failed,Error code: " . $mysqli->errno . ",error message: " . $mysqli->error;
} else {
// Query successful,Processing results
while ($row = $result->fetch_assoc()) {
echo $row['column_name'] . "<br>";
}
}
// Close the database connection
$mysqli->close();
?>
In the above code, we first try to connect to the database and check connect_errno to confirm whether the connection is successful. If the connection fails, $mysqli->connect_error will return specific error messages, helping us quickly locate the problem. If the connection is successful, we continue to perform the SQL query operation and check whether there are errors during the query. If an error occurs, we obtain the specific error code through $mysqli->errno and obtain the detailed error information through $mysqli->error .
1045 - Access denied for user
This error usually indicates an error in the database connection credentials (username or password).
2002 - Can't connect to local MySQL server through socket
This error usually occurs when the MySQL server cannot be connected to the specified socket, which may be caused by the MySQL service not being started or configuration errors.
1049 - Unknown database
This error occurs when the specified database does not exist.
By combining mysqli_connect_errno() and mysqli::$errno , we can effectively obtain and process error information in database connections. Both have their own emphasis. mysqli_connect_errno() is mainly used for handling connection errors, while mysqli::$errno is suitable for a wider range of operation error handling. In actual development, mastering how these two can make our database operations more robust and help quickly troubleshoot and resolve connection errors.