PHP while while语句中的while语句?

时间:2016-09-14 14:22:05

标签: php mysql

Db的db2 到目前为止,代码示例仅列出了所有帖子,但只显示了一条评论? 我到目前为止只编写了一条评论?

请任何有帮助的人我真的需要帮助我是一个编码的小伙子并尽我所能。提前谢谢

<?php
include("../db/db.php");
{

    /*if(isset($_POST["TimeLine"])){*/
    $queryposts = "SELECT * FROM post";
    $run_query = mysqli_query($con,$queryposts);
    while($row = mysqli_fetch_array($run_query)){
        $post_id = $row["post_id"];
        $uid = $row["user_id"];
        $content = $row["content"];
        $like = $row["total_like"];
        $date = $row["date_created"];
        $uidpic = $row["profilepic"];
        $email = $row["email"];
        $pImage = $row["postimage"];
        $pVideo = $row["video"];
        echo "<b>POST: <b/><hr />".
            $content."<hr >"
        ;


        $querycomment = "SELECT * FROM comment WHERE comment_id = $post_id";
        $run_queryb = mysqli_query($con,$querycomment);
        while($row = mysqli_fetch_array($run_queryb)){
            $ComId = $row["comment_id"];
            $Comment = $row["comment"];
            $ComDate = $row["comment_date"];
            $ProfilePicpost = $row["profilepic"];
            echo "<b>Comment: <b/><hr />".
                $Comment."<hr >"
            ;
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您在内循环中覆盖了相同的变量$row。将其名称更改为其他任何内容。

$queryposts = "SELECT * FROM post";
$run_query = mysqli_query($con,$queryposts);

while($posts = mysqli_fetch_array($run_query)){

    // Your post related code
    $post_id = $posts["post_id"];
    $uid = $posts["user_id"];
    // Use $posts instead of $row

    $querycomment = "SELECT * FROM comment WHERE comment_id = $post_id";
    $run_queryb = mysqli_query($con,$querycomment);

    while($comments = mysqli_fetch_array($run_queryb)){

        // Your comment related code
        $ComId = $comments["comment_id"];
        $Comment = $comments["comment"];
        // Use $comments instead of $row

    }
}