如何显示mySQL数据库中的多个项目

时间:2014-06-01 22:15:07

标签: php mysql

您好我正在使用html和php构建一个博客,并且遇到了我的sql问题。在我的博客中,我想显示用户在评论部分中具有相同文章ID的所有评论。在我的数据库中,我通过$ _POST和查询ID,ArticleID,注释保存这些参数。但是,只显示了带有该articleID的数据库中插入的最后一个注释。

这是我正在使用的代码。有人可以帮帮我吗?

if(isset($_POST['submit']))
{
    $comment = htmlentities($_POST["comment"]);
    $articleID = $_GET['artId'];

    $query = "INSERT INTO tbl_comments (comment, ArticleID) VALUES  ('$comment', $articleID)";
    $result = mysqli_query($connection, $query) or die("Error in query: ". mysqli_error($connection));
}

$query1 = "SELECT * FROM tbl_comments WHERE ArticleID = $artId";
$result1 = mysqli_query($connection, $query1) or die("Error in query: ". mysqli_error($connection));

while($row = mysqli_fetch_assoc($result1))
{
    $articleId = $row['ArticleID'];
    $comment = $row['comment'];             
}

if(isset($comment))
{
    echo "<div class='comments'>";
    if (isset($comment))
    {
        echo "<div class='commentName'>";
        echo $comment;
        echo "</div>";
    }

1 个答案:

答案 0 :(得分:0)

变化

while($row = mysqli_fetch_assoc($result1))
{
 $articleId = $row['ArticleID'];
 $comment = $row['comment'];                
}

为:

$comment = '';
while($row = mysqli_fetch_assoc($result1))
    $comment .= $row['comment'] . '<br/>';