duanli9591 2014-08-08 08:25
浏览 67
已采纳

while循环只传递第一个sql行

Source code is showing 16 items as requested by LIMIT. However all are only the first item in the column 'category'. How can i get this to show the first 16 rows of the column, not just the first one.

<div id="contentContainer" class="trans3d">
  <?php include("../scripts/db.php");
    $query="SELECT category FROM stories LIMIT 16"; 
    $result=mysqli_query($connection,$query);
  ?>
     <section id="carouselContainer" class="trans3d">
  <?php   
      while($data=mysqli_fetch_assoc($result)):
            $genre=$data['category'];
            $genre_path=$genre.".php"; 
            $genre_img_path="../images/genre_images/".$genre.".jpg";
  ?>

<a href="$genre_path">
    <figure class="carouselItem trans3d">
        <?php echo $genre;?>
            <img src="<?php echo $genre_img_path;?>" alt=""/>
    </figure>
</a>
<?php 
     endwhile;
     mysqli_close($connection);
 ?>
        </section>

</div>
  • 写回答

2条回答 默认 最新

  • dor2p0520 2014-08-08 08:31
    关注

    change the query to:

    $query = "SELECT DISTINCT category FROM stories LIMIT 16";
    

    This will ensure that you get 16 different categories. Otherwise, the selection of which 16 rows is arbitrary, and it might pick 16 stories that happen to be in the same category.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部