Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES)
Solution 1
That combination of username, host, and password is not allowed to connect to the server. Verify the permission tables (reloading grants if required) on the server and that you're connecting to the correct server.
Solution 2
If youre running wamp update
define("DB_HOST", "localhost");
To your machines ip address (mine is 192.168.0.25);
define("DB_HOST", "192.168.0.25");
You can find it on window by typing ipconfig in your console or ifconfig on mac/linux
Solution 3
Make sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.
Note: 12345 mentioned below is your plain password
GRANT ALL PRIVILEGES ON dbname.* TO 'yourusername'@'%' IDENTIFIED BY '12345';
Solution 4
mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
use DB_HOST instead of DB_SERVER
Solution 5
Look at your code You defined DB_HOST and querying DB_SERVER. DB_USER and DB_USERNAME Use this code
define("DB_SERVER", "localhost");
define("DB_USER", "root");
define("DB_PASSWORD", "");
define("DB_DATABASE", "databasename");
$connect = mysqli_connect(DB_SERVER , DB_USER, DB_PASSWORD, DB_DATABASE);
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
Warning: mysqli_connect(): (HY000/1045): Access denied for user 'username'@'localhost' (using password: YES) in C:\Users\xampp\htdocs\PHP_Login_Script\config.php on line 6
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 10
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 11
Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 15
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 16
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in C:\Users\xampp\htdocs\PHP_Login_Script\login.php on line 19
I'm getting this error above on localhost even if my config file is like this:
<?php define("DB_HOST", "localhost"); define("DB_USER", "root"); define("DB_PASSWORD", ""); define("DB_DATABASE", "databasename"); $db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE); ?>
This used to work, but now it doesn't anymore. Is there any problem with this code or is it not working now? This is the tutorial I am working on right now.
-
Admin over 9 yearssir im only using local host and i have root as username and a blank password..i dont see anything wrong with it because before it is working is it obsolete?that way of coding that is why it is not working?using localhost is like that and root and blank password is always accepted
-
Ignacio Vazquez-Abrams over 9 yearsYour server says otherwise.
-
Admin over 9 yearswait let me check again on how to connect to local host just to be sure il be back on you
-
vlex over 8 yearswhy? What difference does it make how one calls their vars or constants?
-
Jack over 8 yearsIn the question the user passed the wrong constant names.
-
Sathya almost 7 yearsMake sure that your password doesn't have special characters and just keep a plain password (for ex: 12345), it will work. This is the strangest thing that I have ever seen. I spent about 2 hours to figure this out.
-
Chiwda about 6 years@Sathya is right. Don't use cpanel's method of generating a password - apparently some special characters don't work. It took me the same 2 hours - then I found the above comment...
-
Nosajimiki about 6 yearsWhile this answer is the most correct, specifically do no use 12345 since it is one of the top 10 most used passwords in the world, but do keep your password alphanumeric. By adding just a few extra characters something like 'smJ4bJ39kHab29PpfjKq' can still provide equal or better entropy as those cPanel randomly generated passwords, while still excluding special characters.
-
Rakibul Haq over 5 yearsdon't know why this happened for one server, similar password worked in the other server, good catch by the way!! it solved the problem.
-
thisisjaymehta over 3 yearsOn WAMP, one also need to make sure that PHP is connecting to mySQL only. By default it will connect to MariaDB. It can be changed from WAMP tray icon by changing default SQL server to mySQL.
-
RyJ about 3 yearsI CANNOT believe that this worked, but it did and ended the madness I've been going through for the past 3+ hours! Alphanumeric passwords FTW!!
-
techmagister almost 3 yearsI was using MAMP on MacOs and replacing 'localhost' by '127.0.0.1' resolved the issue.
-
Admin over 2 yearsYour answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-
AndyNope over 2 yearsIn my case, I've selected "locally access only" on the server. Sp I did not have permission. Just set it to any Host and voila.
-
KarTo over 2 yearsGreat, thanks Nacho, I saved those two hours.
-
moiamserru over 2 yearsHad to do this on my local Mac in OSX too. Didnt work with localhost. Had to enter 127.0.0.1 instead.
-
Muhammad Asif Raza almost 2 yearsThank you so so much @Sathya and Chiwda I was pulling my hairs till I reached your comments