Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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

SELECT COUNT(id) FROM table

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()

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)

SELECT COUNT(*) AS number FROM table

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top