2013-07-07 70 views
0

PDO的rowCount()方法總是返回零值,即使存在的結果,根據:PDO的rowCout()總是爲零

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

Fromt此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

從文檔引用:如果由關聯的PDOStatement執行的最後一條SQL語句是SELECT語句,則某些數據庫可能會返回該語句返回的行數。但是,不能保證所有數據庫的這種行爲,並且不應該依賴便攜式應用程序。 – Maerlyn

+0

Downvotes - 解釋? – Jack

+0

(不是我,但)可能是因爲這在手冊中有明確的解釋。 – Maerlyn

回答

3

rowCount()返回受上一DELETE,INSERT,或者通過相應的PDOStatement對象執行對象UPDATE語句的行數

它不會select語句工作..

檢查手冊:rowCount()