Chaya Cooper
Chaya Cooper

Reputation: 2530

Username and password are correct but I'm still getting ERROR: SQLSTATE[28000] [1045]

I'm switching from mySql to PDO, but I'm having trouble creating the correct connection to the database. The username and password work in mySql, but I get this error message when I try to connect using the code shown below:

ERROR: SQLSTATE[28000] [1045] Access denied for user 'sean'@'localhost' (using password: NO)

I'm not really sure why it's saying password 'NO' because I'm definitely using the correct password, and there aren't any users named Sean. Is there something wrong with the syntax I'm using for the username or password?

This is the code I'm using (I'm swapping out 'MyPassword' for the actual password)

<?php
session_start(); 
try {
$conn = new PDO('mysql:host=localhost;dbname=MyDatabase', $clickfi4_root, $MyPassword);
  $stmt = $conn->prepare('SELECT * FROM customer_info WHERE id = :id');
  $stmt->execute(array('id' => $id));
  $result = $stmt->fetchAll();
  if ( count($result) ) {
    foreach($result as $row) {
      print_r($row);
    }
  } else {
    echo "No rows returned.";
  }
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}      
?>

Upvotes: 1

Views: 855

Answers (4)

Ergec
Ergec

Reputation: 11824

For future Googlers, I had the same problem just now and I was pretty sure that password was correct. Yes password was correct indeed but the problem is how I generate password and how I keep password in config file.

If you use a random password generator like me make sure you don't have $ dollar sign in your password.

If you have $ in you password then make sure you keep you password in config file like this

$pass = 'randomchars$morerandom';

but not like this

$pass = "randomchars$morerandom";

Upvotes: 0

Chaya Cooper
Chaya Cooper

Reputation: 2530

Turns out that the syntax in the net tuts tutorial I was using was slightly wrong. It works when I remove the '$' before the username and password :-)

$conn = new PDO('mysql:host=localhost;dbname=MyDatabase', clickfi4_root, MyPassword);

Upvotes: 0

Anton
Anton

Reputation: 4018

The error message said:

(using password: NO)

Which means a password was not used in the login attempt.

Check the value of $MyPassword.

Also, try using an account other than the root. It's not the best practice anyway.

Upvotes: 1

Clayton Bell
Clayton Bell

Reputation: 430

Sometimes [email protected] does the trick instead of username@localhost.

Upvotes: 0

Related Questions