I'm building a simple log-in function for a site, and I've come across some trouble. When I try to log in to the site, I keep getting the error that username and password doesn't match. This is of course an error I've defined myself, but the problem is that I shouldn't get it in this case.
I printed the calculated hash for the password, and made sure that this step was properly performed, which it was. After this, I ran
SELECT * FROM members WHERE user='username' AND pass='hashed_password';
directly toward my MySQL database, supplying the username and hash directly. This returned the desired values. My PHP code looks like this:
$dsn = "mysql:host=localhost;dbname=my_db";
$u = "localhost";
$pdo = new PDO($dsn,$u);
$query = $pdo->prepare("SELECT * FROM members WHERE user=':username' AND pass=':hashed_pass'");
$query->execute(array(':username'=>$username,':hashed_pass'=>$hashed_pass));
$result = $query->fetch();
if($result){
//Log in user
}else{
//Print error
}
This does not return anything, even though I supply the correct values, it keeps giving me the error. I'm not sure what might cause the problem, but if I'd make a guess I'd say that it has to do with the charsets. My database uses UTF-8. Although, I still got the desired results from MySQL when querying the database directly though SSH with ISO-8859 encoding.. So I'm really not sure. Any help would be much appreciated!