多个 while 语句 - 如何
我的脚本有问题。
我有这个:
<?php
$r=mysql_query("SELECT * FROM memberships WHERE id!='".$userdata['membership']."'");
while($md = mysql_fetch_assoc($r)):
echo '<td class="header">'.$md['name'].'</td>';
endwhile;
?>
那有效。但如果在页面的下方,我会添加以下内容:
<?php
while($md = mysql_fetch_assoc($r)):
echo '<td class="result">'.$md['number_of_ads'].'</td>';
endwhile;
?>
它不会输出任何内容。为什么?
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>';
endwhile;
?>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是因为没有更多的元素可供获取。不确定您想要完成什么,但如果您想从头开始取回数据,则需要在第二个代码块之前添加以下内容:
这会将内部指针重置到开头。
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:
That will reset the internal pointer to the beginning.
一旦从
$r
获取了所有行,对其的连续调用将不会返回任何行。因此,当第一个 while 循环结束时,第二个循环中的查询结果中没有任何内容可获取
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
fetch_association 函数从结果集中提取行。第一个 while 循环结果集。如果您想循环该数据两次,则需要重置数据。
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.