PDO获取记录行数

20-08-18 17:58 字数 346 阅读 786

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();
1人点赞>
关注 收藏 改进 举报
1 条评论
排序方式 时间 投票
Up骚年

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

请登录后发表评论