Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WHILE loop 1

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
0
0
US
Hi all,

I'm running a while loop on a MySql query:

while ($row = mysql_fetch_assoc($rresult)){
extract($row);
}

Question: I want to run the while loop until a counter hits a specific number. Can this be done? I tried:

while ($counter<=10 && $row = mysql_fetch_assoc($rresult)){
extract($row);
}

But it didn't work... Any ideas how I can do this?
 
Use a FOR loop instead.

Code:
$counter=5;  [green]set the counter to where you want it to stop.[/green]
for($i=0;$i<=$counter;$i++){
$row=mysql_fetch_assoc($result);
extract($row);
}

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Unless the remaining rows will also used elsewhere, would be better to limit your query itself :
Code:
[b]select[/b] * [b]from[/b] [green][i]thetable[/i][/green] [red][b]limit[/b] 10[/red]

Feherke.
 
Agreed. If you want to limit the number of rows, do it from the query itself. That way you don't actually pull out rows you wont use.

If you are going to use the rest of the rows somewhere, then either solution works.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

vacunita, there may be fewer rows than the count. I have no idea how [tt]extract()[/tt] will behave if $row is [tt]false[/tt], but I would not try :
Code:
$counter=5;
for($i=0;$i<=[red]min([/red]$counter[red],mysql_num_rows($result))[/red];$i++){
$row=mysql_fetch_assoc($result);
extract($row);
}


Feherke.
 
Hi

Oops, now I see the $count is starting with 0 and the operator is <=. ( By the way, that will process 6 rows. ) So it should be [tt]mysql_num_rows()[red]-1[/red][/tt].

Feherke.
 
Thanks for the input everyone! There's a bit more to the problem... The while loop contains a second loop.. I need the first one to stop based on a counter that's contained in the second loop. So I can't use "limit" in my query because I don't know the value of the limit until I get to the inner loop. Kinda strange I know...
 
A bit more... Whenever I use the FOR loop PHP barfs. I get this message:

extract() [function.extract]: First argument should be an array in 'path to file'...
 
GOT IT! Just had to change syntax a bit.. Here's what worked:

while($counter!=10 && $row = mysql_fetch_assoc($rresult))

 
I'm afraid to ask what you are doing that requires a condition so ambiguous as $counter!=10.

So as long as $counter isn't 10 everything is fine and dandy??

It can be 53457484367 and it still works?

As for my for loop suggestion, seeing as i now have no idea what you are trying to do, it probably doesn't work in your scenario.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
The 10 was just to simplify things a bit.. It ends up being a dynamic variable. For some odd reason <=10 didn't work. When I changed it to !=10, it did... Not sure what's up with that but I suppose it serves the purpose.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top