PHP query the number of records that meet the conditions in the database (two implementation methods)_PHP tutorial

WBOY
Release: 2016-07-21 15:13:19
Original
1088 people have browsed it

When you need to output the number of website user registrations, or determine whether there are duplicate records before inserting data, you need to obtain the number of records queried by MySQL that meet the conditions.
The first method: direct statistics when querying

Copy the code The code is as follows:

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

Second method: take out first, then count
Copy code The code is as follows:

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

However, when using MySQL's COUNT(*) directly when the amount of data is huge, the efficiency advantage is very significant, because the latter The former requires secondary calculation, so it is better to use the former to count the number of data items.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326514.htmlTechArticleWhen you need to output the number of website user registrations, or determine whether there are duplicate records before inserting data, you need to obtain the satisfaction The number of records of the conditional MySQL query. The first method: check...
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!