Mugdho Hassan
Mugdho Hassan

Reputation: 11

Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

When I try to run my PHP code on localhost, then error given given below appears:

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO) in C:\xampp\htdocs\Final Project CSE-391\project\config.php:2
Stack trace:
#0 C:\xampp\htdocs\Final Project CSE-391\project\config.php(2): PDO->__construct('mysql:host=loca...', 'root', '')
#1 C:\xampp\htdocs\Final Project CSE-391\project\index.php(2): include('C:\xampp\htdocs...')
#2 {main} thrown in C:\xampp\htdocs\Final Project CSE-391\project\config.php on line 2

I need suggestion to solve this issue.

Upvotes: 0

Views: 8280

Answers (4)

Time Lord
Time Lord

Reputation: 1

If you used a password generator to create your password as I sometimes do. You may get a password like this: LJdKE$7swNZ00En2iLwFS3NSvq# It will work fine when you use PhpMyAdmin.

If you get an SQLSTATE[HY000] [1045] Access denied for user ... error. Then there is a character in your password that PHP does not like.

In my case, it was the '#' at the end of the password. The solution, change the password.

Upvotes: 0

Kurniawan Wijaya
Kurniawan Wijaya

Reputation: 41

If the phpMyAdmin port is not the default port, then on the host, it should not only include localhost but also specify the port, for example, localhost:3311.

port phpmyadmin

example

define("HOST", "localhost:3311");
define("USER", "root");
define("PASS", "");
define("DBNAME", "phprestaurant");

Upvotes: 0

also
also

Reputation: 11

Solution

I fixed this by pointing to the SQL port: $host = 'localhost:3307' instead of $host='localhost'.

Example

$host = 'localhost:3307';
$db = 'php_crud';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host; dbname=$db; charset=$charset";
$username = 'root';
$password = '1234';
$pdo = new PDO($dsn, $username, $password);

Upvotes: 1

David Kariuki
David Kariuki

Reputation: 1756

This is a wrong credential issue. Check the values you used on the browser to log into PHPMyAdmin to ensure it is correct before putting it in the connection config.

You could have entered the wrong password or leaving password to blank after having set a password.

Upvotes: 1

Related Questions