while循环只重复第一行多次

时间:2018-05-20 18:57:20

标签: php

我正在尝试输出数据库查询结果的所有行,但我的while循环只重复多次第一行。它仅重复第一行预期结果的总次数。我需要帮助。

$sql = "SELECT * FROM review WHERE lid = '".$_GET['id']."'";
$result = $conn -> query($sql);
$row = $result -> fetch_assoc();

if (mysqli_num_rows($result) == 0){
    echo "<div class='no-comment'>No Comments found!</div>
    <button class='btn btn-default btn-lg center-block' data-toggle='modal' data-target='#Review'>Review Lecturer Now!</button>
    ";
}else{
    while($result -> fetch_assoc()){
        echo"
            <div class='comment'>
                <div class='reviewer-name'>".$row['rid']."<span class='review-date'>".$row['reviewDate']."</span><hr>
                <p>".$row['review']."</p></div>
            </div>
            ";
    }
}

1 个答案:

答案 0 :(得分:3)

更改

while($result -> fetch_assoc()){

while($row = $result -> fetch_assoc()){

同时从第3行删除第一个$row = ...