PHP queries the number of records in the database that meet the criteria for of

  • 2020-05-27 04:37:29
  • OfStack

When you need to output the number of registered website users, or determine if there are duplicate records before inserting the data, you need to get the number of records that satisfy the criteria for MySQL queries.
The first method: query time direct statistics
 
$sql="SELECT COUNT(*) AS count FROM TABLE WHERE id='$id'"; 
$result=mysql_fetch_array(mysql_query($sql)); 
$count=$result['count']; 

The second method: take out first and count later
 
$sql="SELECT * FROM TABLE WHERE id='$id'"; 
$result=mysql_fetch_array(mysql_query($sql)); 
$count=count($result);// or $count=mysql_num_rows($result); 

However, COUNT(*) directly used by MySQL has a significant efficiency advantage of 10 points when there is a large amount of data. Since the latter requires two computations, it is better to use the former for the statistics of the number of data bars.

Related articles: