pdo里面怎么得到所查询的数据的条数?

WBOY
Release: 2016-06-06 20:07:27
Original
1771 people have browsed it

pdo里面怎么得到所查询的数据的条数?谢谢

回复内容:

pdo里面怎么得到所查询的数据的条数?谢谢

<code>$rows = $pdo->query('select * from expense where state = 2')->fetchAll(PDO::FETCH_ASSOC);
echo count($rows);</code>
Copy after login

http://cn2.php.net/mysqli
mysqli中,num_rows存储了select查询到的行数,affected_rows存储了查询(insert/update/delete)中受影响(改动)的行数.
http://cn2.php.net/pdo
pdo中,用count计算结果集数组得到select查询到的行数,rowCount()存储了查询(insert/update/delete)中受影响(改动)的行数.
值得注意的是,update时,如果更新的内容跟原来的内容一样,mysqli的affected_rows,pdo的rowCount()也会返回0.

<code>$pdostatement = $pdo->query($query);
$number = $pdostatement->rowCount();</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!