Control of mysql_fetch_array

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
arpowers
Forum Commoner
Posts: 76
Joined: Sun Oct 14, 2007 10:05 pm
Location: san diego, ca

Control of mysql_fetch_array

Post by arpowers »

Hey!

here is my problem....

I am writing a script that needs to pull all users from a relational table. I would like to return the total number of users affiliated but I would also like to limit the array fetched to a suitable number to be displayed on the screen (say.. 6)..

so if I use

Code: Select all

$total_num = mysql_num_rows($result);

while($row = mysql_fetch_array($result)){
    //this will get data for every user... i don't want to do that.
    $data[] = $row;
}
Whats the best way to control the amount of the total actually fetched? I would try and use LIMIT {$users_displayed} in my query but that trumps the total users affiliated question....
Suggestions?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

Your description sounds contradictory. Either you want to return all the rows or you don't. If you need two sets, it would take two queries. Perhaps you want to return all the rows but only display some? You would do that by using a for loop (or a while loop) as you echo the data to be displayed. What is it that you are trying to do??
User avatar
arpowers
Forum Commoner
Posts: 76
Joined: Sun Oct 14, 2007 10:05 pm
Location: san diego, ca

Post by arpowers »

Thanks for the reply.. sorry if I wasn't clear... but maybe your right...


I want to:
1. get a total number of users affiliated (e.g. you have 22 users affiliated)
2. only output a certain lower number. (e.g. user1 - user2 - user3- user4 etc...)

how is this done.:)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

You could make a fake column using COUNT() in MySQL, but I'm no MySQL genius by any means... there's probably a better way to do it.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

I think LIMIT is what you're looking for -- I don't understand why that wouldn't work. check out http://dev.mysql.com/doc/refman/5.0/en/select.html for the syntax
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

He wants to know the total number of results as well as limit the result set....in one query.

If it can't be done I suggest using two queries, one to get the total (using COUNT() in MySQL), and one to fetch the rows with a LIMIT.
Post Reply