[英]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();
我不确定我是做错了还是不做我应该做的事情,但是我正在使用
rowCount()
尝试为查询结果计算数据库中的所有行,例如: $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.
结果显示6应该只有2时才显示6。似乎好像是在计算数据库中的所有行,而不是我查询的结果。
What am I doing wrong? 我究竟做错了什么?
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'];
}
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 当然,您将需要绑定正确的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.