0

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";
?>
3
  • 2
    does the user have the assigned rights to access this database? does the database exists? btw, The default user in xampp is root with no password. Commented May 23, 2018 at 19:51
  • yes. the database exists. But I am not sure whether the user has the right to access the database. Ho to check it out or how to give the access? Commented May 23, 2018 at 19:53
  • try running mysql -u root -p from cmd Commented May 23, 2018 at 21:06

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.