pdo在类中获取查询只返回一行

时间:2014-05-16 06:21:29

标签: php mysql pdo

编辑:大家指出我覆盖了$ Html变量。我今晚有点喝酒而错过了。

我正在为我的网站开发自己的自定义论坛,由于某种原因,代码只返回一行。论坛上有两个“帖子”,当回显此功能的结果时,只返回最新的帖子。

    function displayTopics($tid)
{
    $sql = "SELECT message_body, poster FROM thread_messages WHERE threadID = :tid";
    $que = $this->db->prepare($sql);
    $que->bindParam(':tid', $tid);
    try{ 
        $que->execute();
        while($row = $que->fetch(PDO::FETCH_OBJ))
        {
            $username = $this->getUsername($row->poster);
            $html = "<div class='message'>
                        <div class='userInfo'>
                            <img width='50' height='50' />
                            {$username}
                        </div>
                        <div class='body'>
                        {$row->message_body}
                        </div>
                    </div>";    
        }
    }catch(PDOException $e){}
                $html .= 
                "<form action='reply.php' method='post'><input type='hidden' name='tid' value='{$tid}'>".
                "<textarea name='replybody'>Reply...</textarea>".
                "<input type='submit' value='reply'>".
                "</form>";
                return $html;

}

1 个答案:

答案 0 :(得分:0)

我搞砸了,我的坏。循环应该看起来像这样

        while($row = $que->fetch(PDO::FETCH_OBJ))
    {
        $username = $this->getUsername($row->poster);
        $html .= "<div class='message'>
                    <div class='userInfo'>
                        <img width='50' height='50' />
                        {$username}
                    </div>
                    <div class='body'>
                    {$row->message_body}
                    </div>
                </div>";    
    }

我重写了$ Html变量。