我尝试用PHP连接 Mysql。当我不使用数据库时,它工作得很好。但是当我尝试连接数据库时会出现以下错误:
Warning: mysqli::__construct(): (HY000/1044): Access denied for user ''@'localhost' to database 'db_test' in C:\xampp\htdocs\Projects\DemoProject\demo2.php on line 13 Connection failed: Access denied for user ''@'localhost' to database 'db_test'
我的PHP代码如下:
<?php
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "db_test";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
echo "Connected Successfully";
?>
I actually used the wrong username. To check the username and servername just run the query given below-
select CURRENT_USER();
In general, username set as root
and server name set as localhost
by default.
Replace the user with your username and pass with your password for database and run the below command grant and flush privileges from root user.
grant all privileges on db_test.* to 'user'@'localhost' identified by 'pass';
flush privileges;