开发者

Multiple while statements - How to

I have a problem with my script.

I have this:

<?php
$r=mysql_query("SELECT * FROM memberships WHERE id!='".$userdata['membership']."'");

while($md = mysql_fetch_assoc($r)):
  echo '<td class="header">'.$md['name'].'</td>';
end开发者_如何转开发while;
?>

That works. But if further down the page, I add this:

<?php
while($md = mysql_fetch_assoc($r)):
  echo '<td class="result">'.$md['number_of_ads'].'</td>';
endwhile;
?>

It doesn't output anything. Why?


Maybe because there's no more elements to fetch. Not sure what you are trying to accomplish, but if you want to fetch the data back from the beginning, you need to add this before the second block of code:

mysql_data_seek($r, 0);

That will reset the internal pointer to the beginning.


Once you have fetched all the rows from $r, successive call to it will not return any row.

So when the first while loop ends, there is nothing to fetch from the query result in the second loop


The fetch_association function draws rows from a resultset. The first while loops over the result set. If you want to loop that data twice you need to reset the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜