duannuo7878 2013-10-04 15:21
浏览 26
已采纳

根据ID显示所有字段

I am building a blog and trying to show all comments that apply to the post.

Each post has an ID and each comment is stored with the post ID.

here is my code:

<?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  
                }        

    ?>

When I run this code the page only shows one comment, although my DB has 3 comments with the correct ID.

  • 写回答

4条回答 默认 最新

  • dswy34539 2013-10-04 15:43
    关注

    I would consider using mysqli_ as mysql_ has been depreciated. I'd also consider using a while loop, hopefully this will help:

    <?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>";
    
    
     ?>
    

    I haven't tested this for bugs, but let me know if you like further information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧