Reputation: 340
I'm not sure why this bit of code:
if($sth->rows == 0){
echo "Incorrect username or password - 1";
}
is pulling the error Undefined property: PDOStatement::$rows. This works just fine on a different PHP script that I basically had changed only a few things. I do however also receive the echo "Incorrect username or password -1" meaning that if statement did run.
Here is the full PHP code.
<?php
$lusername = $_POST['username'];
$lpassword = $_POST['password'];
//Hashing password
$cost = 10;
$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
$salt = sprintf("$2a$%02d$", $cost) . $salt;
$hash = crypt($lpassword, $salt);
// Create connection
$dsn = 'mysql:dbname=weblupne_template3;host=localhost';
$username = 'somethingFreakingCrazyMagical';
$password = 'somethingEvenMoreCrazyFreakingMagical';
try {
$db = new PDO($dsn, $username, $password); // also allows an extra parameter of configuration
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // set the PDO error mode to exception
} catch(PDOException $e) {
die('Could not connect to the database:<br/>' . $e);
}
//Where to select from
$sth = $db->prepare('SELECT password FROM login WHERE username = :username LIMIT 1');
$sth->bindParam(':username', $lusername);
$sth->execute();
$user = $sth->fetch(PDO::FETCH_OBJ);
if($sth->rows == 0){
echo "Incorrect username or password - 1";
}
else{
//Tests if correct
echo $user->hash;
if ( hash_equals($user->password, crypt($lpassword, $user->password)) ) {
echo "You check out";
}
else{
echo "Incorrect username or password - 2";
}
}
?>
Upvotes: 1
Views: 5208
Reputation: 183
You have to use $sth->rowCount()
, there is no property named rows
.
Upvotes: 1