PHP Error: Solution to Unable to Connect to Database
In the process of using PHP development, we often encounter the problem of being unable to connect to the database. This is a very common mistake, but it causes a lot of trouble to developers. This article will introduce some common solutions and provide corresponding code examples to help developers quickly solve the problem.
First, you should check whether the database connection information is correct. Typically, database connection information includes hostname, username, password, and database name. Correct database connection information ensures a normal connection to the database.
For example, when we connect to the MySQL database, we can use the following code example:
<?php $servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "mydatabase"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } echo "连接成功"; ?>
Sometimes, the database cannot be connected. Because there is a problem with the database server. We can check the status of the database server with the following code example:
<?php $servername = "localhost"; $username = "root"; $password = "123456"; $connection = @mysqli_connect($servername, $username, $password); if (!$connection) { die("数据库服务器连接失败:" . mysqli_connect_error()); } else { echo "数据库服务器连接成功"; } mysqli_close($connection); ?>
If the database server is not started, PHP will not be able to connect to the database. We can check whether the database service is running with the following code example:
<?php if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) { echo 'MySQLi扩展未加载'; } else { echo 'MySQLi扩展已加载'; } if (!function_exists('mysqli_connect')) { echo 'MySQLi函数未定义'; } else { echo 'MySQLi函数已定义'; } ?>
Database connection problems may also be caused by insufficient permissions. Make sure the database user has sufficient permissions to access the database.
The following is a code example for checking database permissions:
<?php $servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "mydatabase"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } $sql = "SELECT * FROM users"; $result = $conn->query($sql); if ($result->num_rows > 0) { while ($row = $result->fetch_assoc()) { echo "ID: " . $row['id'] . ",姓名: " . $row['name'] . "<br>"; } } else { echo "没有数据"; } $conn->close(); ?>
Summary:
Failure to connect to the database is one of the common PHP errors, but we can check the database connection information , database server status, whether the database service is started, and database permissions to solve this problem. We hope that the solutions and code samples provided in this article can help developers solve the problem of being unable to connect to the database as soon as possible.
The above is the detailed content of PHP error: Unable to connect to the database solution. For more information, please follow other related articles on the PHP Chinese website!