0

我正在建立一个博客并尝试显示适用于该帖子的所有评论。

每个帖子都有一个 ID,每个评论都与帖子 ID 一起存储。

这是我的代码:

<?php
 $con = mysql_connect("localhost","cl49-XXX-b","X");
if (!$con) 
  {
   die('Could not connect: ' . mysql_error());
   }  
mysql_select_db("cl49-XXX-b", $con)or die( "Unable to select database line 873");
$result=mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'") or die('Error on Line 215 :'.mysql_error());

 echo " <ul class='comments'> "; // first row beginning
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
    $row = mysql_fetch_array($result);
    $name = $row['name'];
    $email = $row['email'];
    $comment = $row['comment'];
    $created=$row['created'];   

 echo"   <li>
                <div class='comment'>
                    <div class='thumbnail'>
                        <img class='avatar' alt='' src='img/avatar.jpg'>
                    </div>
                    <div class='comment-block'>
                        <div class='comment-arrow'></div>
                        <span class='comment-by'>
                            <strong>$name</strong>
                            <span class='pull-right'>
                                <span> <a href='#'><i class='icon-reply'></i> Reply</a></span>
                            </span>
                        </span>
                        <p>$comment</p>
                        <span class='date pull-right'>$created</span>
                    </div>
                </div>  ";

echo "   </li>"; // it's time no move to next row  
                }        

    ?>

当我运行此代码时,该页面仅显示一条评论,尽管我的数据库有 3 条带有正确 ID 的评论。

4

4 回答 4

1

我会考虑使用 mysqli_ 因为 mysql_ 已被贬值。我也会考虑使用while循环,希望这会有所帮助:

<?php
$DBServer = 'localhost';
$DBUser   = 'xxxx';
$DBPass   = 'xxxx';
$DBName   = 'xxxx';

$mysqli = new mysqli($DBServer, $DBUser, $DBPass, $DBName);

if ($mysqli->connect_errno) {
    echo "Failed to connect to the database: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

$query = "SELECT * FROM blogcomments WHERE ID='". $ID ."'";

echo " <ul class='comments'> ";

if ($result = $mysqli->query($query)) {

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

<li>
    <div class='comment'>
        <div class='thumbnail'>
            <img class='avatar' alt='' src='img/avatar.jpg'>
        </div>
        <div class='comment-block'>
            <div class='comment-arrow'></div>
                <span class='comment-by'>
                    <strong><?php echo $row['name']; ?></strong>
                    <span class='pull-right'>
                        <span><a href='#'><i class='icon-reply'></i> Reply</a></span>
                    </span>
                </span>
                <p><?php echo $row['comment']; ?></p>
                <span class='date pull-right'><?php echo $row['created']; ?></span>
            </div>
        </div>
    </div>
</li>

<?php

    } $result->close();
} $mysqli ->close();

echo "</ul>";


 ?>

我还没有测试过这个错误,但如果你想了解更多信息,请告诉我。

于 2013-10-04T15:43:15.487 回答
0

当你这样做时:

mysql_query("SELECT * FROM blogcomments WHERE ID='".$ID."'")

您在数据库中只选择了一条评论。也许你必须这样做:

mysql_query("SELECT * FROM blogcomments WHERE post_ID='".$post_ID."'")
于 2013-10-04T15:28:08.310 回答
0

因为您正在选择 ID 字段值$ID在表 blogcomments 中的行。

我猜您将评论所连接的引用的 post_id 存储在一个名为“post_id”之类的字段中。您最好将您的选择查询指向该字段;)

如果您将表格的结构放在问题中,我可能会有所帮助:)

于 2013-10-04T15:28:36.820 回答
0

你没有关闭你的<ul>. 可能只是 HTML 格式问题?

于 2013-10-04T15:30:08.053 回答