Home > php教程 > php手册 > body text

PHP查询数据库中满足条件的记录条数(两种实现方法)

WBOY
Release: 2016-06-13 11:55:17
Original
823 people have browsed it

在需要输出网站用户注册数,或者插入数据之前判断是否有重复记录的时候,就需要获取满足条件的MySQL查询的记录数目。
第一种方法:查询时候直接统计

复制代码 代码如下:


$sql="SELECT COUNT(*) AS count FROM TABLE WHERE id='$id'";
$result=mysql_fetch_array(mysql_query($sql));
$count=$result['count'];


第二种方法:先取出,后统计

复制代码 代码如下:


$sql="SELECT * FROM TABLE WHERE id='$id'";
$result=mysql_fetch_array(mysql_query($sql));
$count=count($result);//或者$count=mysql_num_rows($result);


不过直接使用MySQL的COUNT(*)在数据量庞大的时候,效率的优势是十分显著的,因为后者需要二次计算,所以还是最好使用前者进行数据条数的统计。
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 Recommendations
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!