doumu9019 2013-06-26 00:25
浏览 51
已采纳

查询不显示信息:PDO,MYSQL,PHP

I've been banging my head over this simple PDO query for hours. What is the correct way to fetch this type of information from MySQL multiple databases. In this query should Fetch or FetchAll be used? as It's querying multiple databases.

Below is my attempt to convert the deprecated MySQL to PDO.

MySQL.

    public function Comments($post_iD) {
        $query = mysql_query("
                                SELECT C.com_id, C.uid_fk, C.comment, C.created, U.username 
                                FROM comments C, users U 
                                WHERE U.status = '1' 
                                AND C.uid_fk = U.uiD 
                                AND C.msg_id_fk = '$msg_id'
                                ORDER by C.com_id"); or die(mysql_error());

            while( $row = mysql_fetch_array($query) )
                $data[] = $row;
            if( !empty( $data ) ){
                return $data;
            }
    }

PDO:

    PUBLIC FUNCTION Comments( $post_iD ){
        $sth = $this->db->prepare("
                                SELECT C.com_id, C.uid_fk, C.comment, C.created, U.username 
                                FROM comments C, users U 
                                WHERE U.status = '1' 
                                AND C.uid_fk = U.uiD 
                                AND C.msg_id_fk = ?
                                ORDER by C.com_id");

        $sth->execute(array($post_iD));

        $data = $this->fetch();
        return $data;
        }
    }

This is how I'm displaying the database.

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

        if( $commentsarray ){
            foreach($commentsarray as $data){
                $com_id     =   $data['com_id'];
                $comment    =   tolink(htmlcode($data['comment'] ));
                $time       =   $data['created'];
                $mtime      =   date("c", $time);
                $username   =   $data['username'];
                $com_uid    =   $data['uid_fk'];
                $cface      =   $Wall->Profile_Pic($com_uid);
    ?>
                <div class="stcommentbody" id="stcommentbody<?php echo $com_id; ?>">
                    <div class="stcommentimg">
                        <img src="<?php echo $cface;?>" class="small_face" alt=<?php echo $username;?>">
                    </div> 

                    <div class="stcommenttext">
                        <?php echo clear($comment); ?>
                    </div>
            </div>
<?php
        } 
    }
?>
  • 写回答

1条回答 默认 最新

  • dougou6213 2013-06-26 00:32
    关注

    You want (using fetchAll() on your query $sth)

    $data = $sth->fetchAll();
    

    instead of (fetch() on your db connection $this->)

    $data = $this->fetch();
    

    as
    fetch() Fetches the next row from a result set
    where
    fetchAll() Returns an array containing all of the result set rows.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信