Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES)

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES)

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES)

When you encounter Warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES), the error message is actually quite specific about what went wrong. The PHP script attempted to establish a connection to the MySQL server running on your machine, but the authentication credentials provided did not match any valid user account in the database.

In the snippet you shared, the code attempts to connect using localhost, the username root, and the literal string "password":

$conn = mysql_connect("localhost", "root", "password") or die(mysql_error());

The error indicates that a user named root exists on the system, but the password provided ("password") was rejected by the MySQL server. This is almost certainly not a bug in your login logic or input validation code; it is a configuration issue with your XAMPP installation. By default, many XAMPP distributions set the root password to an empty string (no password) or a specific hash like root. However, if you have manually changed this password in phpMyAdmin or via the command line without updating your PHP connection strings, or if the server is configured to require authentication for local connections, the literal string "password" will fail.

To resolve this, you must align your PHP code with the actual database configuration. The safest and most immediate fix is to inspect the root password in phpMyAdmin. Navigate to http://localhost/phpmyadmin in your browser, click on the Users tab (or Users accounts depending on your version), and locate the user root. If the "Password" field is empty but your script sends "password", you have a mismatch. Either update the password in phpMyAdmin to match what your PHP code expects, or change your PHP code to use an empty string if no password is required.

However, looking at the rest of your code reveals a more critical architectural issue that needs addressing immediately. You are using the mysql_* functions (mysql_connect, mysql_select_db, mysql_query). These functions were deprecated in PHP 5.5 and completely removed in PHP 7.0. If your XAMPP environment is running anything newer than 7.4, this script will not execute at all, regardless of the connection error you are seeing. The extension was removed specifically because it does not support prepared statements, making it vulnerable to SQL injection attacks.

Your current login query:

$query = "SELECT * FROM profile WHERE username = '$username' AND password = '$password'";

is dangerously susceptible to injection if a user inputs special characters into the uname or pword fields. Even though you are validating for empty strings, this approach does not protect against malicious input like ' OR '1'='1.

To move forward correctly, you should migrate to either MySQLi or PDO with prepared statements. Here is how you would refactor the connection and query using MySQLi:

// Create a new MySQLi connection
$conn = new mysqli("localhost", "root", "", "ngchoonching"); // Adjust password if set

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$username = $_POST['uname'];
$password = $_POST['pword'];

// Use prepared statements to prevent SQL injection
$stmt = $conn->prepare("SELECT * FROM profile WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows == 1) {
    // Login successful logic here
}

This approach not only solves the authentication error by allowing you to input a secure password (or an empty one if that is your configuration) but also secures your application against data breaches. If you are starting a new project, consider using a modern framework like Laravel, which handles database connections and prepared statements out of the box via its Eloquent ORM or Query Builder.

For further guidance on securing PHP applications and migrating legacy code to modern standards, refer to the resources available at https://laravelcompany.com.

Our network