如何统计查询结果的所有行出现次数
How to count all row occurrences of query results
我不确定我是否做错了或没有包括我应该做的事情,但我正在使用 rowCount()
来尝试计算数据库中的所有行以获取查询结果,如下所示: $count_total_friend = $friend_stmt->rowCount();
。结果显示 6 而它应该只有 2。它似乎在计算我数据库中的所有行,而不是我的查询结果。
我做错了什么?
$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'];
}
您的查询有问题
SELECT *
FROM friends
WHERE friend_one or friend_two = ?
AND status = ?
应该是这样的
SELECT *
FROM friends
WHERE (friend_one = ? or friend_two = ?)
AND status = ?
当然你需要绑定正确的参数
我不确定我是否做错了或没有包括我应该做的事情,但我正在使用 rowCount()
来尝试计算数据库中的所有行以获取查询结果,如下所示: $count_total_friend = $friend_stmt->rowCount();
。结果显示 6 而它应该只有 2。它似乎在计算我数据库中的所有行,而不是我的查询结果。
我做错了什么?
$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'];
}
您的查询有问题
SELECT *
FROM friends
WHERE friend_one or friend_two = ?
AND status = ?
应该是这样的
SELECT *
FROM friends
WHERE (friend_one = ? or friend_two = ?)
AND status = ?
当然你需要绑定正确的参数