Paul Paul - 10 days ago 5
MySQL Question

How to count all row occurrences of query results

I am not sure if I am doing this wrong or not including something I should be, but I am using

rowCount()
to try and count all the rows in my database for the query results, like this:
$count_total_friend = $friend_stmt->rowCount();
. The results are showing 6 when it should be only 2. It seems as if it is counting all the rows in my database, rather than my queried results.

What am I doing wrong?

enter image description here

The

$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'];
}

Max Max
Answer

There is a problem with your query

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

It should be something like

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

Of course you will need to bind the right params