Sohid Ullah
Sohid Ullah

Reputation: 75

MySql Access denied for user ''@'localhost' to database

I am trying to connect with Mysql using PHP. It works fine when I do it without a database. But gives me the following error when I try to connect with a database.

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'

My PHP code is given below.

<?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";
?>

Upvotes: 0

Views: 15472

Answers (2)

Sohid Ullah
Sohid Ullah

Reputation: 75

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.

Upvotes: 0

user8406805
user8406805

Reputation:

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;

Upvotes: 1

Related Questions