从循环中的数据库调用时不显示注释

时间:2013-06-29 00:28:48

标签: php mysql pdo

我正在从带有PDO的MySQL中发表评论,显示限制为2条注释,但查询不会检索注释,也不能var_dump print_r,因为它确实打印了任何内容。我认为它应该是查询本身的一个问题,认为它已经无数小时无效,所以我想我会问更有经验的程序员。

页面上没有输出数据,我不确定它是second_count变量还是post_iD变量的问题。

1。我是否正确使用了while循环?

2。最好使用while循环或foreach?

第3。 var_dump和print_r函数是否因为错误而无效     查询或在PHP.INI中看起来是什么?

以下是我用PDO提取数据的方法。

PUBLIC FUNCTION Comments( $post_iD,$second_count ){
    if($second_count){
        $sth = $this->db->prepare("
                                SELECT C.com_id, C.uid_fk, C.comment, C.created, U.username, U.photo
                                FROM comments C, users U 
                                WHERE U.status = '1' 
                                AND C.uid_fk = U.uiD 
                                AND C.msg_id_fk = :postiD
                                ORDER BY C.com_id ASC LIMIT :second, 2");
        $sth->execute(array(':postiD' => $post_iD, ':second' => $second_count));

        while( $row = $sth->fetchAll())
            $data[] = $row;
        if(!empty($data)){
            return $data;
       }
   }
}

以下是我试图在我的页面上显示信息的方式,

<?php
    $commentsarray      = $Wall->Comments( $post_iD, 0 );
    $x=1; 

    if($x){
        $comment_count  = count($commentsarray);
        $second_count   = $comment_count-2;

        if($comment_count>2){
?>
        <div class="comment_ui" id="view<?php echo $post_iD; ?>">
            <a href="#" class="view_comments" id="<?php echo $post_iD; ?>">View all<?php echo $comment_count; ?> comments</a>
        </div>

<?php
        $commentsarray  = $Wall->Comments( $post_iD, $second_count );
        }
    }

    if( $commentsarray ){
        foreach($commentsarray as $data){
            $com_id     = $data['com_id'];
            $comment    = tolink(htmlcode($data['comment'] ));
            $time       = $data['created'];
            $mtime      = date("g:i", $time);
            $username   = $data['username'];
            $com_uid    = $data['uid_fk'];
            $photo      = $data['photo'];

?>
            <div class="stcommentbody" id="stcommentbody<?php echo $com_id; ?>">
                <div class="stcommentimg">
                    <img src="<?php echo $photo;?>" class="small_face">
                </div> 

                <div class="stcommenttext">
                    <?php if($uiD == $com_uid || $uiD == $post_iD ){ ?>
                        <a class="stcommentdelete" href="#" id="<?php echo $com_id;?>" title="Delete Comment"></a>
                    <?php } ?>

                    <div class="stmessage"><?php echo clear($comment); ?></div>
                </div>
                <div class="stime"><a href='' title='<?php echo $time;?>' class="timeposted"><?php echo $mtime;?> — Like</a></div>
            </div>
<?php
        } 
    }
?>

1 个答案:

答案 0 :(得分:0)

1.仅仅是这样:

$data = $sth->fetchAll();

if(!empty($data) AND count($data) > 0){
   return $data;
}

您无需while即可使用fetchAll

2.我最好在你的每个查询中使用Exception,然后你就可以看到是否有任何错误。

e.g

try
{
   //your PDO stuffs here
}
catch (PDOException $e)
{
  echo $e->getMessage();
}