未定义 属性:PDOStatement::$rows

Undefined property: PDOStatement::$rows

我不确定为什么会有这段代码:

if($sth->rows == 0){
    echo "Incorrect username or password - 1";
}

正在拉取错误 Undefined 属性: PDOStatement::$rows。这在我基本上只改变了一些东西的不同 PHP 脚本上工作得很好。但是我确实也收到了 echo "Incorrect username or password -1" 意思是 if 语句做了 运行.

这是完整的 PHP 代码。

<?php
$lusername = $_POST['username'];
$lpassword = $_POST['password'];

//Hashing password
$cost = 10;

$salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');

$salt = sprintf("a$%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";
    }
}
?>

您必须使用 $sth->rowCount(),没有 属性 名为 rows