为什么我的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 -->
php
douhua1760
douhua1760 使用while循环而不是while循环。并且不要使用mysql开始使用mysql。*apis
接近 4 年之前 回复
douou9786
douou9786 使用循环使用所有。
接近 4 年之前 回复

3个回答

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.

drkjzk3359
drkjzk3359 谢谢!! 那工作!!
接近 4 年之前 回复

You are setting variables used for display outside the loop. They are set with the first result and won't change after.

You should do something like this instaed :

 <?php
     $sql_query=mysql_query($sql);

     while ($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'];


        echo '....';
        echo $job;
        // or directly
        echo $post['job'];
    }
 ?>
<?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);

 ?>


        <!-- Blog - Start -->
        <?php   while ($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'];

         ?>
        <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 }) ?>
        <!-- Blog - End -->
doushan9415
doushan9415 第二个支架??? })
接近 4 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问