我正在尝试在两个标签中获得相同的广告ID(ad_id)(A和B部分)。当我选择A部分时它正常工作,但是当我选择B部分时,我无法获得数据。
代码如下:
<div><?php $connect=mysql_connect("localhost","root","");
$db=mysql_select_db("bil",$connect) ;
$query=mysql_query("select ad_id from ad limit 5"); ?>
<ul class="nav nav-tabs" id="myTab">
<li ><a href="#sectionA">Section A</a></li>
<li class="active"><a href="#sectionB">Section B</a></li>
</ul>
<div class="tab-content">
<div id="sectionA" class="tab-pane fade in active">
<h3>Section A<?php while($r=mysql_fetch_assoc($query)){ echo $r['ad_id'].'-';} ?></h3>
</div>
<div id="sectionB" class="tab-pane fade active">
<h3>Section B<?php while($r1=mysql_fetch_assoc($query)){ echo $r1['ad_id'].'-';} ?></h3>
</div>
</div>
</div>
答案 0 :(得分:0)
mysql_fetch_assoc()返回下一行,并将内部指针移动到下一行。返回所有行后,返回null。所以在你的第二个while循环中,你试图循环null。你应该切换到mysqli而不是弃用的mysql
重写你的代码:
<?php
$r=mysqli_fetch_all($query, MYSQLI_ASSOC);
$r1=mysqli_fetch_all($query, MYSQLI_ASSOC);
?>
<div id="sectionA" class="tab-pane fade in active">
<h3>Section A<?php while($r){ echo $r['ad_id'].'-';} ?></h3>
</div>
<div id="sectionB" class="tab-pane fade active">
<h3>Section B<?php while($r1){ echo $r1['ad_id'].'-';} ?></h3>
</div>