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();
}
?>
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 🙂