Posted By

brent-man on 02/05/08


Tagged

mysql php count


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

fackz
mh5777
vali29
joethermal


MySQL count rows


 / Published in: PHP
 

Only because I have forgotten this code 3 times today...

If there is a better/faster way, please comment.

  1. <?php
  2.  
  3. // connect to the database first
  4.  
  5. dbconnection();
  6.  
  7. // query
  8. $result = mysql_query("SELECT * FROM tablename");
  9.  
  10. $rows = mysql_num_rows($result);
  11.  
  12. // results
  13. echo $rows;
  14.  
  15. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: adix on February 6, 2008

Maybe you want to do something like:

SELECT COUNT(id) AS CNT FROM table WHERE condition

CNT will then have the count as it should be. Much faster than your solution. Waaaaaay faster.

Posted By: Unreal on July 30, 2009

Thats true adix but not under all circumstances. I prefer this over COUNT

Posted By: gerardcorr on October 30, 2009

if your gonna use mysql num rows for just counting rows then its best to use the id field in the SELECT instead of the * , but all depends what your doing.

You need to login to post a comment.