Home  >  Article  >  Backend Development  >  pdo里面怎么得到所查询的数据的条数?

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

WBOY
WBOYOriginal
2016-06-06 20:07:271771browse

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

回复内容:

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

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

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>
Statement:
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