这是我的功能:
$result_display = mysql_query("SELECT * FROM Events ORDER BY TimeStamp DESC LIMIT $y, 20");
$result_comment = mysql_query("SELECT * FROM Comments");
while($row_display = mysql_fetch_array($result_display)){
echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";
while($row_comment = mysql_fetch_array($result_comment)){
echo $row_comment['com_details'];
}
}
HTML 输出类似于:
<div id='id12' class='eventdiv' data-sort='238781>Time: 238781</div>
<div id='id13' class='eventdiv' data-sort='238784>Time: 238784</div>
<div id='id14' class='eventdiv' data-sort='238785>Time: 238785</div>
<div id='id15' class='eventdiv' data-sort='238789>Time: 238789</div>
<div id='id16' class='eventdiv' data-sort='238791>Time: 238791</div>
<div id='id17' class='eventdiv' data-sort='238795>Time: 238795</div>
These are the comment details...
但我希望输出是这样的:
<div id='id12' class='eventdiv' data-sort='238781>Time: 238781</div>
These are the comment details...
<div id='id13' class='eventdiv' data-sort='238784>Time: 238784</div>
These are the comment details...
<div id='id14' class='eventdiv' data-sort='238785>Time: 238785</div>
These are the comment details...
<div id='id15' class='eventdiv' data-sort='238789>Time: 238789</div>
These are the comment details...
<div id='id16' class='eventdiv' data-sort='238791>Time: 238791</div>
These are the comment details...
<div id='id17' class='eventdiv' data-sort='238795>Time: 238795</div>
These are the comment details...
我认为我在 while 语句中包含 while 语句的方式会像上面的 HTML 一样显示它,但事实并非如此。
最佳答案
在您第一次运行 $result_comment 结果后,您必须使用 mysql_data_seek( $result_comment )
重置它。所以:
while($row_display = mysql_fetch_array($result_display)){
echo "<div id='id'" . $row_display['ID'] . " class='eventdiv' data-sort='".$row_display['TimeStamp']."'>".$row_display['Title']." TIME: ".$row_display['TimeStamp']."</div>";
while($row_comment = mysql_fetch_array($result_comment)){
echo $row_comment['com_details'];
}
mysql_data_seek( $result_comment );
}
关于php - 如何在 While 语句中运行 While 语句以从数据库中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6351416/