2016-11-28 87 views
0

我不知道如果我做這個錯誤或不包括我應該的東西,但我正在使用rowCount()來嘗試和計數我的數據庫中的所有行查詢結果,像這樣:$count_total_friend = $friend_stmt->rowCount();。結果顯示爲6時,它應該只有2.它似乎在計算我的數據庫中的所有行,而不是我查詢的結果。如何計算查詢結果的所有行出現

我在做什麼錯?

enter image description here

$friend_status = 2; 
    $friend_sql = " 
     SELECT * 
     FROM friends 
     WHERE friend_one or friend_two = ? 
     AND status = ? 
    "; 
    $friend_stmt = $con->prepare($friend_sql); 
    $friend_stmt->execute(array($user_id, $friend_status)); 
    $friend_total_rows = $friend_stmt->fetchAll(PDO::FETCH_ASSOC); 
    $count_total_friend = $friend_stmt->rowCount(); 
    foreach ($friend_total_rows as $friend_total_row) { 
     //$select_friend_1 = $friend_total_row['friend_one']; 
     //$select_friend_2 = $friend_total_row['friend_two']; 
     //$friend_status  = $friend_total_row['status']; 
     //$friend_status_date = $friend_total_row['date']; 
    } 

回答

1

沒有您查詢的是什麼

SELECT * 
    FROM friends 
    WHERE friend_one or friend_two = ? 
    AND status = ? 

它應該是這樣的

SELECT * 
    FROM friends 
    WHERE (friend_one = ? or friend_two = ?) 
    AND status = ? 

當然你需要綁定正確的參數

+0

它仍然顯示4,如果我這樣做。參數將是'WHERE friend_one = 24或friend_two = 24 AND status = 2' – Paul

+0

對,忘了括號。請看我的更新 – Max

+0

剛剛發現。謝謝您的幫助!\ – Paul

相關問題