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条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。