douchujian8124
douchujian8124
采纳率100%
2018-05-23 19:45

求助:数据库的MySql访问被拒绝

已采纳

我尝试用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";
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongyishen5796 dongyishen5796 3年前

    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.

    点赞 评论 复制链接分享
  • dsk95913 dsk95913 3年前

    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;
    
    点赞 评论 复制链接分享

相关推荐