How to get total of table rows?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

How to get total of table rows?

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
Last edited by CoderGoblin on Tue Feb 01, 2005 9:49 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
User avatar
pthomas
Forum Commoner
Posts: 68
Joined: Wed Jan 19, 2005 11:28 am
Location: Cincinnati, OH

Post 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
Post Reply