【问题标题】:Query not displaying information : PDO, MYSQL, PHP查询不显示信息:PDO、MYSQL、PHP
【发布时间】:2013-06-26 00:25:02
【问题描述】:

我已经为这个简单的 PDO 查询绞尽脑汁好几个小时了。从 MySQL 多个数据库中获取此类信息的正确方法是什么。 在这个查询中应该使用 Fetch 还是 FetchAll?因为它正在查询多个数据库。

以下是我将已弃用的 MySQL 转换为 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;
        }
    }

这就是我显示数据库的方式。

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

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    您想要(在您的查询中使用fetchAll() $sth

    $data = $sth->fetchAll();
    

    而不是(fetch() 在您的数据库连接上$this-&gt;

    $data = $this->fetch();
    

    作为
    fetch()Fetches the next row from a result set
    在哪里
    fetchAll()Returns an array containing all of the result set rows.

    【讨论】:

    • 感谢肖恩的回答。它只是给我致命错误:调用 $data = $this->FetchAll(); 上的未定义方法 Wall_Updates::fetchAll();有任何想法吗? 正如我之前尝试过的那样
    • 您需要在查询$sth 上执行fetchAll(),而不是在您的数据库连接$this-&gt; 上。所以应该是$data = $sth-&gt;fetchAll();
    • 同意 +1。或者,使用while 循环(如您的mysql 代码)和fetch()
    • 你是正确的,完美的答案谢谢。 +1 并接受回答。
    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-06
    • 2013-07-18
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多