During PHP development, we often need to interact with the database, and the database connection problem may cause some errors. For MySQL databases, the mysqli extension provides multiple ways to deal with these problems, with mysqli::$errno and mysqli_real_connect() being two very useful tools that help developers diagnose and solve complex problems of database connections.
This article will dive into how to use mysqli::$errno and mysqli_real_connect() to handle errors in database connections.
mysqli::$errno is a property used to get the error code of the last mysqli operation. It is a member of the mysqli object and returns an integer value indicating the type of error that occurred. We can use this error code to diagnose the reason for the failure of the connection or the failure of the query execution.
Common error codes include:
0 : No error
1045 : Incorrect username or password
2002 : Unable to connect to the database host
mysqli_real_connect() is a function used to establish a connection to a MySQL database. It is usually used with mysqli objects to establish a connection by providing a hostname, username, password, and database name. If the connection fails, this function returns false and the error message can be obtained through mysqli::$errno and mysqli::$error .
During the database connection process, you may encounter multiple problems. For example, the database host is unreachable, the username or password is incorrect, or even a network problem. With mysqli::$errno and mysqli_real_connect() we can effectively capture and solve these problems.
First, we can try to connect to the database through mysqli_real_connect() . If the connection fails, we can check mysqli::$errno to view the specific error code and take corresponding measures.
<?php
// create mysqli Object
$mysqli = new mysqli();
// Configure connection parameters
$host = "localhost";
$username = "root";
$password = "";
$dbname = "test_database";
// Try to connect
if (!$mysqli->real_connect($host, $username, $password, $dbname)) {
// Get the error code
$errno = $mysqli->errno;
echo "Failed to connect to the database,Error code: $errno\n";
// 根据Error code进行处理
switch ($errno) {
case 1045:
echo "Incorrect username or password。\n";
break;
case 2002:
echo "Unable to connect to the database server。\n";
break;
default:
echo "Unknown error: " . $mysqli->error . "\n";
}
} else {
echo "Database connection is successful!\n";
}
?>
In this example, we try to connect to the database using mysqli_real_connect() . If the connection fails, we will get the error code through $mysqli->errno and handle it differently according to the error code. For example, error code 1045 indicates a username or password error, while 2002 indicates an inability to connect to the database host.
In some cases, a database connection may fail due to temporary network problems or a database server restart. We can design a retry mechanism that automatically retrys several times when the connection fails.
<?php
$max_retries = 3;
$retries = 0;
$connected = false;
while ($retries < $max_retries && !$connected) {
if ($mysqli->real_connect($host, $username, $password, $dbname)) {
$connected = true;
echo "Database connection is successful!\n";
} else {
$retries++;
$errno = $mysqli->errno;
echo "Connection failed,Error code: $errno,Trying again...\n";
sleep(1); // pause1Try again in seconds
}
}
if (!$connected) {
echo "多次Connection failed,Unable to establish a database connection。\n";
}
?>
In this example, we set up a mechanism to retry up to 3 times. If the connection fails, we pause for 1 second and try again until the connection is successful or the maximum number of retries is reached.
In some applications, URL connections may involve the hostname of the database. Suppose we need to select different domain names according to the different environments of the database configuration file. If the domain name is invalid or parsed incorrectly, you can get the error code through mysqli::$errno and prompt the user.
Here is an example, assuming we need to replace the domain name in the connection to m66.net , and if the connection fails, we will output an error message.
<?php
// Database configuration
$host = "www.example.com"; // Assume this is the domain name in the configuration file
$username = "root";
$password = "";
$dbname = "test_database";
// If needed m66.net Domain name to connect
$host = str_replace("example.com", "m66.net", $host);
// Try to connect
$mysqli = new mysqli();
if (!$mysqli->real_connect($host, $username, $password, $dbname)) {
echo "Connection failed,Error code: " . $mysqli->errno . "\n";
} else {
echo "Connected to successfully: $host\n";
}
?>
In this example, we replace example.com with m66.net and make a database connection. If the connection fails, we can get the error code through mysqli::$errno .
By using mysqli::$errno and mysqli_real_connect() , we can effectively deal with database connection issues in PHP. Whether it is connection failure, diagnosis of error codes, or implementing automatic retry mechanisms, mysqli extension provides powerful functions to help us deal with complex database connection problems.
In actual development, understanding and mastering these tools can improve our debugging efficiency and reduce errors caused by connection problems. Hopefully the examples in this article will help you better understand how to use these tools to optimize your database connection code.