doushi3819244 2016-09-23 13:29
浏览 146
已采纳

为什么我的PHP while循环重复相同的数据库结果?

It is displaying six posts, and I have 6 items in my database. The issue is that it is only displaying the first one, and repeating it five more times. This is my first time writing PHP so please don't get too advanced with terminology!

<?php 

    $pagetitle = "My Portfolio | Projects";

    $header = $_SERVER['DOCUMENT_ROOT'];
    $header .= "/includes/header.php";
    include_once($header);

    $sql="SELECT * FROM projects ORDER BY job DESC";
    $sql_query=mysql_query($sql);
    $post = mysql_fetch_array($sql_query);
    $job = $post['job'];   
    $category = $post['category'];
    $title = $post['title'];
    $inside = $post['imageinside'];
    $outside = $post['imageoutside'];
    $body = $post['body'];
    $description = $post['description'];

?>


            <!-- Blog - Start -->
            <?php do {?>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 blog blog_altered blog_left">
                <div class="row">
                    <!-- Blog Image - Start -->
                    <div class=" col-lg-6 col-md-6 col-sm-10 col-xs-12  pic inviewport animated delay1" data-effect="fadeIn">
                        <img alt="blog-image" class="img-responsive" src="<?php echo "$outside" ?>">
                    </div>
                    <!-- Blog Image - End -->
                    <!-- Blog Info - Start -->
                    <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 inviewport animated delay1" data-effect="fadeIn">
                        <div class="info">
                            <span class="date"><?php echo "$category" ?></span>
                            <h4 class="title"><a href="<?php echo "$job" ?>.php"><?php echo "$title" ?></a></h4>
                            <p><?php echo "$description" ?></p>
                            <a class="btn btn-primary text-on-primary"  href="<?php echo "$job" ?>.php">Read More</a>
                        </div>
                    </div>
                    <!-- Blog Info - End -->
                </div>
            </div>
            <?php } while ($post = mysql_fetch_array($sql_query)) ?>
            <!-- Blog - End -->
  • 写回答

3条回答

  • dsf22567 2016-09-23 13:34
    关注

    That is because you're already assigning it at the top of your script and using those variables later on. So the next time you pass the while it doesn't use the freshly fetched data, but simply the data from your previously set code.

    Simply get rid of this part

    $post = mysql_fetch_array($sql_query);
    $job = $post['job'];   
    $category = $post['category'];
    $title = $post['title'];
    $inside = $post['imageinside'];
    $outside = $post['imageoutside'];
    $body = $post['body'];
    $description = $post['description'];
    

    Change your do-while to a 'normal' while loop, and change your variables inside the html bit to the post ones. So <?php echo "$category" ?> with <?php echo "$post['category']" ?> etc.

    With that said, you'll need this to get this part working. But it would be better to use a while loop, not a do, while. The difference between them being that a while loop will only execute when the data passed to the loop is actually holding something. Do-while will always execute ., and after that check for valid stuff inside your while. So when the database has no results the html block is still printed once.

    You would also make a good choice to look into mysqli or pdo for database connections. mysql is no longer the preferred way (deprecated since 5.5 and removed in php 7)

    Lastly, you could do with mysql_fetch_assoc if you're going to use the associative keys only.

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

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了