I’m having trouble connecting to MySQL through PHP in my XAMPP environment. When I try to run my PHP script, I keep getting this error:
Fatal error: Uncaught mysqli_sql_exception: Access denied for user ‘root’@‘localhost’ (using password: YES) in C:\xampp\htdocs\TestScript.php:8
What’s weird is that I can log into MySQL just fine through the XAMPP control panel using the same credentials. The username and password work there without any problems. But when my PHP code tries to connect, it always throws this access denied error.
I’ve tried resetting the password multiple times and even created new user accounts, but nothing fixes it. The connection works fine in phpMyAdmin too. It’s only when I try to connect through my PHP scripts that this happens.
Does anyone know what might be causing this? I’m pretty new to web development so I might be missing something obvious.
yea, i agree with that! also, check if your my.cnf file has any unusual settings. sometimes, the password can be empty, or you might need to specify the port explicitly in your connection script. hope this helps!
sounds like a php extension issue tbh. check if mysqli or pdo_mysql extensions are properly loaded in your php.ini file - sometimes xampp doesnt enable them by default and you get weird auth errors instead of proper extension missing warnings.
This error usually indicates a mismatch between your connection parameters and the actual MySQL configuration. Since you mentioned the credentials work in phpMyAdmin, double-check that your PHP script is using exactly the same host, username, and password values. One common issue I encountered was having extra whitespace or hidden characters in the connection string that weren’t visible in the code editor. Another possibility is that your XAMPP installation has different authentication plugins enabled - MySQL 8.0 uses caching_sha2_password by default which can cause compatibility issues with older PHP versions. Try creating a new user specifically for your PHP application through phpMyAdmin and grant it the necessary privileges, then test with those credentials instead of root.
I had this exact same issue when I started with XAMPP. The problem is usually that XAMPP sets up MySQL with no password for the root user by default, but your PHP script is trying to authenticate with a password. Check your connection code and try setting the password parameter to an empty string instead of whatever password you think you’re using. Also make sure you’re using ‘localhost’ as the host and not ‘127.0.0.1’ - I’ve seen cases where one works but not the other in XAMPP setups. If you actually did set a password through phpMyAdmin, try restarting both Apache and MySQL services in the XAMPP control panel after making the change.