PDO行Count()始终为零

时间:2013-07-07 18:35:51

标签: php pdo sqlite

PDO的rowCount()方法总是返回零值,即使有结果,根据:

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

从这个PHP代码:

               $_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);

我明白了:

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

如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

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

它不适用于select语句..

检查手册:rowCount()