Jump to content


Photo

Multiple while($r=mysql_fetch_array) loops not working!


  • Please log in to reply
6 replies to this topic

#1 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 12 July 2003 - 11:52 PM

Hello.

Near the top of my code, I have a:

[php:1:b3f9f9e1f6]while($r=mysql_fetch_array($query_events)) { // cycle through events
// soon to be code here
}[/php:1:b3f9f9e1f6]

(the $query_events has already been defined)
Then, on a lower part of my code I have another:

[php:1:b3f9f9e1f6]while($r=mysql_fetch_array($query_events)) { // cycle through events
// lots of code here
}[/php:1:b3f9f9e1f6]

For some reason the first one will work, but not the second one.

Any reason why this might be happening?

Thanks.
DEFINE("YOU","Spending Too Much Time On The Computer");

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 July 2003 - 12:09 AM

hm. how many rows does that query return?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 13 July 2003 - 12:13 AM

Only two... I still can\'t figure it out. Want my entire page code?
DEFINE("YOU","Spending Too Much Time On The Computer");

#4 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 13 July 2003 - 12:29 AM

Okay... well it seems if I display only one of those two at the top, then it will only display the other of the two on the bottom.

Basically, once I select it from the database it won\'t let me select it again... is there any way to, uh... unselect it after you\'re done or something?

Thanks.
DEFINE("YOU","Spending Too Much Time On The Computer");

#5 dammitjanet

dammitjanet
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationUK

Posted 13 July 2003 - 12:34 AM

is $query_events being redefined between the two code snippets?

if its not, and you are working on the same result set, then the first while loop exhausts the query results, and the second one will never be true and will not run.

you will have to either store the results within your first qry in an array or whatever you choose, or run the query again to get the result set. Id advocate the first choice.

#6 Avalanche

Avalanche
  • Members
  • PipPipPip
  • Advanced Member
  • 73 posts

Posted 13 July 2003 - 01:22 AM

Hmm... what if I had two indentical queries, but with different names? Would that work (I would test it, but I\'m not on my PC that I use for PHP)?

Thanks once again, I very much appreciate it.
DEFINE("YOU","Spending Too Much Time On The Computer");

#7 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,667 posts
  • LocationLos Angeles, CA USA

Posted 13 July 2003 - 02:21 AM

Yes it would work. But you could also just execute the query again, with a mysql_query call. You need a query to get a result set. Once the result set has been fetched, as was stated, it\'s basically useless.

If this is any static value, the advice of loading it into an array is a very good one. If you can avoid doing a requery you\'re keeping load off the database, and it should also perform better. If you are changing the database, or have an expectation that it might have changed then the re-query is understandable.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users