Page 1 of 1

Control of mysql_fetch_array

Posted: Thu Oct 18, 2007 6:50 pm
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?

Posted: Thu Oct 18, 2007 7:49 pm
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??

Posted: Thu Oct 18, 2007 8:27 pm
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.:)

Posted: Thu Oct 18, 2007 11:10 pm
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.

Posted: Thu Oct 18, 2007 11:37 pm
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

Posted: Fri Oct 19, 2007 12:28 am
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.