我有一个有效的功能,但不完全符合预期。这是因为while ($row = mysqli_fetch_assoc($result))
仅在特定用户的数据库中已有一条记录时执行。
例如,当我尝试为ID = 1的用户添加第一条评论时,它会将数据推送到数据库表中,但不会显示。然后,如果我尝试添加第二条评论,它最终会显示它以及之后添加的任何其他记录。
这是我的功能代码。
function displayUserReviews() {
global $link;
$query = "SELECT * FROM user_reviews WHERE receiver_id='".$_GET['userid']."' LIMIT 10";
$result = mysqli_query($link, $query);
$row = mysqli_fetch_assoc($result);
if (mysqli_num_rows($result) == 0) {
echo "<div class='alert alert-info'>This user does not have any comments on his profile yet.</div>";
} else {
$queryUser = "SELECT * FROM users WHERE id='".$row['sender_id']."'";
$queryUserResult = mysqli_query($link, $queryUser);
$rowUser = mysqli_fetch_assoc($queryUserResult);
$userName = $rowUser['username'];
$userId = $rowUser['id'];
}
while ($row = mysqli_fetch_assoc($result)) {
$comment = $row['comment'];
echo "<div class='col' id='userReviewsDiv'><em><p>~$comment</p></em>
<hr>
<span class='userReviews'>Author: </span><a href='?page=profile&userid=$userId'>$userName</a>
</div>";
}
}