# PDO获取记录行数

**URL:** <https://www.shiqidu.com/t/topic/714>\
**Category:** 开发调优\
**Tags:** MySQL\
**Created:** [2020 年8 月 18 日 09:58 UTC](https://www.shiqidu.com/t/topic/714 "2020-08-18T09:58:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

作者： ![yeqiu](https://www.shiqidu.com/user_avatar/www.shiqidu.com/yeqiu/32/14_2.png) [@yeqiu](https://www.shiqidu.com/u/yeqiu)\
发布日期： [2020 年8 月 18 日 09:58 UTC](https://www.shiqidu.com/t/topic/714/1 "2020-08-18T09:58:15Z")

</div>

SQL如下

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

```

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

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

```

然后通过 fetchColumn 查询

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

```

---

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2020 年9 月 2 日 08:17 UTC](https://www.shiqidu.com/t/topic/714/2 "2020-09-02T08:17:07Z")

</div>

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