views:

105

answers:

5

How can i get the number of rows like mysql_num_rows but without having a mysql resource to work from, because i don't know the future scale of my webapp and it could take an hour just to get the number of rows ;)

thanks!

+1  A: 
SELECT COUNT(*) FROM table WHERE ...

See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count for differences between COUNT(*) and COUNT(id)

Tomba
+4  A: 

Make an SQL query with COUNT, e.g.:

SELECT COUNT(id) FROM table
Felix Kling
+8  A: 

SELECT COUNT(*) AS num_rows FROM tblName; - and just retrieve num_rows column from that statement.

Crozin
thanks, returns in correct formet :)
tarnfeld
+1  A: 

Just in case you have a LIMIT clause in your "actual" sql query and want to know the toal number of items (e.g. when doing some kind of pagination) you might also be interested in SQL_CALC_FOUND_ROWS and FOUND_ROWS()

VolkerK
A: 

SELECT COUNT(*) AS number FROM table

Will return 1 row with a field called number which holds the number of rows in the table.

Rowno