Jump to content


Photo

multiple mysql_fetch_array() calls


  • Please log in to reply
4 replies to this topic

#1 themule

themule
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 August 2006 - 02:46 PM

Hello.. I'm trying to populate multiple select boxes with the same query, but only the first iteration works.

<select>
while($row = mysql_fetch_array($myQry)){
  <option>echo $row['name']
}
</select>
**Populates correctly
<select>
while($row = mysql_fetch_array($myQry)){
  <option>echo $row['name']
}
</select>
**No data

Any thoughts?

#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 11 August 2006 - 03:35 PM

use mysql_data_seek($myQry,0); before while loop

#3 markt

markt
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 11 August 2006 - 04:14 PM

Or how about unsetting $myQry?
Or mysql_close() ....

#4 themule

themule
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 12 August 2006 - 02:06 AM

mysql_data_seek worked.. thanks sasa!


#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 12 August 2006 - 02:10 AM

i'm going to suggest that you be nicer to your database, and stop making it work so hard.  plug everything from your returned rows into an array and loop through that rather than forcing the database to go through the same set of results over and over:

<?php
while($row = mysql_fetch_array($myQry))
{
  $options[] = $row['name'];
}

echo '<select name="something">';
foreach ($options AS $name)
  echo "<option>$name</option>";
echo '</select>';
?>

everytime you want to loop through your results, use the foreach() loop on the local $options variable rather than the while() MySQL loop.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users