0

我正在使用 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
        } 
    }
?>
4

1 回答 1

0

1.就这样:

$data = $sth->fetchAll();

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

您无需while使用fetchAll.

2.我最好在每个查询中使用异常,然后您可以查看是否有任何错误。

例如

try
{
   //your PDO stuffs here
}
catch (PDOException $e)
{
  echo $e->getMessage();
}
于 2013-06-29T00:41:21.423 回答