0

The rowCount() method of PDO always returns zero value, even if there's results, according to:

count($sth->fetch(PDO::FETCH_ASSOC)) > 1

Fromt this PHP code:

               $_user = "a";
               $_pass = "b";
         $query = "select user,password from login where user = :user and password = :pass";
    $conn = new PDO("sqlite:" .Config::$db_file);
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $sth = $conn->prepare($query);
    $sth->bindParam(':user', $_user);
    $sth->bindParam(':pass', $_pass);
    $sth->execute();
    $result = $sth->fetch(PDO::FETCH_ASSOC);
    echo "rowCount() = ",$sth->rowCount(),"\n";
    echo 'count($result) = ', count($result), "\n";
    print_r($result);

I get:

rowCount() = 0 count($result) = 2 Array ( [user] => a [password] => b )

How to fix this?

4

1 回答 1

3

rowCount()返回受相应 PDOStatement 对象执行的最后一个 DELETE、INSERT 或 UPDATE 语句影响的行数

它不适用于选择语句..

检查手册:rowCount()

于 2013-07-07T18:38:34.647 回答