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

时间:2016-09-23 13:29:08

标签: php

它显示了六个帖子,我的数据库中有6个项目。问题是它只显示第一个,并重复五次。这是我第一次编写PHP,所以请不要使用术语太高级了!

<?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 个答案:

答案 0 :(得分:2)

这是因为您已经在脚本顶部分配它并稍后使用这些变量。因此,下次您通过时它不会使用刚刚获取的数据,而只是使用先前设置的代码中的数据。

简单地摆脱这部分

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

将你的do-while更改为'normal'while循环,并将html位内的变量更改为post变量。 所以<?php echo "$category" ?><?php echo "$post['category']" ?>等。

话虽如此,你需要这个才能让这部分工作。但是,最好使用while循环,而不是do。它们之间的区别在于while循环只会在传递给循环的数据实际存在时执行。 Do-while将始终执行。之后检查有效内容。因此,当数据库没有结果时,html块仍然会打印一次。

您还可以选择使用mysqli或pdo进行数据库连接。 mysql不再是首选方式(自5.5以来已弃用并在php 7中删除)

最后,如果您只想使用关联键,则可以使用mysql_fetch_assoc

答案 1 :(得分:1)

<?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 -->

答案 2 :(得分:1)

您正在设置用于循环外显示的变量。它们是使用第一个结果设置的,并且在之后不会改变。

你应该做这样的事情:

 <?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'];
    }
 ?>