PDO获取记录行数

SQL如下

select * from `users` where create_time >= '2020-06-01 00:00:00'

使用 rowCount() 方法是错误的,rowCount() 返回的是受影响行数。正确的做法是使用相同的条件放到 select count(*) 语句中。

select count(*) from `users` where create_time >= '2020-06-01 00:00:00'

然后通过 fetchColumn 查询

$totalCount = $pdo->query("select count(*)  from `users` where {$sqlWhere}")->fetchColumn();

rowCount返回受影响行数,fetchColumn从结果集中的下一行返回单独的一列。
如果你的SQL是这样的 select count(*) from goods group by user_id,那么你需要用使用 rowCount方法,才能获取正确的记录条数。