根据ID显示所有字段

时间:2013-10-04 15:22:00

标签: php html mysql database

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

每个帖子都有一个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 个答案:

答案 0 :(得分: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>";


 ?>

我没有对此漏洞进行测试,但如果您想了解更多信息,请与我们联系。

答案 1 :(得分:0)

执行此操作时:

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

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

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

答案 2 :(得分:0)

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

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

如果你把表格的结构放在问题中,我可能会帮忙:)

答案 3 :(得分:0)

您没有关闭<ul>。可能只是一个HTML格式问题?