Page 1 of 1

How to get total of table rows?

Posted: Tue Feb 01, 2005 9:40 am
by pthomas
Ok,I've been looking around and I stumbled upon a php mysql command called mysql_num_fields. And it looks like it'll do what I want, I just want to get the total number of rows that are in a table. It looks like how this command works, it actually needs you to get all items in a table, return them and then it totals them up to see how many there are. Is there a faster way? Or is that the wrong way to go with it?

While I'm tapping into your knowledge, is there a way to have mysql just randomly grab a set number of items out of a table?

Tnaks,
Paul

Posted: Tue Feb 01, 2005 9:48 am
by CoderGoblin
http://www.php.net/mysql_num_rows

As for specific number of rows I believe SELECT x WHERE z=t LIMIT n can be used (not sure its been ages since I used MySQL).

Regards

Posted: Tue Feb 01, 2005 9:48 am
by feyd

Code: Select all

SELECT COUNT(*) FROM table_name
fetch like normal..

Code: Select all

SELECT * FROM table_name ORDER BY RAND() LIMIT 10

Posted: Tue Feb 01, 2005 11:29 am
by pthomas
Thanks for the info guys. I think I'm going to go with

Code: Select all

SELECT * FROM table_name ORDER BY RAND() LIMIT $number
For grabbing a set number of items out of the DB. It worked pretty darn good and simplified what I was trying to do.

Paul