2016-11-28 78 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