We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

brent-man on 02/05/08


Tagged

mysql php count


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

mh5777


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.

You need to login to post a comment.