duagfgfn1981 2013-04-27 11:03
浏览 33
已采纳

除了在OOP方法中使用foreach之外,如何从数据库中获取数据?

I'm using PDO to connect the database and using OOP method in coding

this is how to get the posts and comments

class MyWeb{
public function SelectStatus($user_id){
        try{
            $DBC = new DBConnector();

            $query = "SELECT * FROM users U, posts P where P.user_id_fk=U.user_id and U.user_id=:user_id_fk";

            $params = array(":user_id_fk"=>$user_id);


            $result = $DBC->SelectArray($query,$params);

            if($result){
                return $result;
            } else throw new Exception("Post not selected!");
        }catch(Exception $e){
            echo "Caught Exception: ".$e->getMessage();
            return null;
        }
    }
public function SelectComment($post_id){
        try{
            $DBC = new DBConnector();

            $query = "SELECT * FROM comments C, users U WHERE C.user_id_fk = U.user_id and C.post_id_fk = :post_id_fk";


            $params = array(":post_id_fk"=>$post_id);

            $result = $DBC->SelectArray($query,$params);

            if($result){
                return $result;
            } else throw new Exception("Comment not selected!");
        }catch(Exception $e){
            echo "Caught Exception: ".$e->getMessage();
            return null;
        }
    }
}

and this how to call the functions and display posts and comments

<?php
        $NewStatus = $session->SelectStatus($user_id);

        if(!empty($NewStatus)){
            foreach($NewStatus as $data){
                $username = $data->username;
                $post = $data->post;
                $post_id = $data->post_id;
                                echo "".$username." | ".$post."";

                               $NewComment = $session->SelectComment($post_id);

                if(!empty($NewComment)){
                    foreach($NewComment as $cdata){
                        echo $cdata->comment;
            }
        }
    }
}
?>

But sadly I always get error -> Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\RIO\RIO\RAI\session_rai\includes\db.php on line 14

So, Any solutions for this case? Thanks.

  • 写回答

2条回答 默认 最新

  • duangang4001 2013-04-27 11:44
    关注

    Your class structure is wrong.

    1. You don't need DBConnector class
    2. Never create more than one connection to database with same credentials
    3. Most of code in MyWeb class is useless too

    So, create a PDO connection, then instantiate MyWeb class and then get your data

    class MyWeb{
    
        function __construct($dbc)
        {
            $this->dbc = $dbc;
        }
    
        public function SelectStatus($user_id)
        {
            $query = "SELECT * FROM users U, posts P 
                          WHERE P.user_id_fk=U.user_id and U.user_id=?";
            $stmt  = $this->dbc->prepare($query);
            $stmt->execute(array($user_id));
            return  $stmt->fetchAll();
        }
    
        public function SelectComment($post_id)
        {
            $query = "SELECT * FROM comments C, users U 
                          WHERE C.user_id_fk = U.user_id and C.post_id_fk = ?";
            $stmt  = $this->dbc->prepare($query);
            $stmt->execute(array($user_id));
            return  $stmt->fetchAll();
        }
    }    
    

    same with output

    <?php
    $pdo = new PDO(... params);
    $myweb = new MyWeb($pdo);
    $NewStatus = $myweb->SelectStatus($user_id);
    foreach($NewStatus as $row)
    {
         echo $row['username']." | ".$row['post'];
         $NewComment = $myweb->SelectComment($post_id);
         foreach($NewComment as $cdata){
             echo $cdata['comment'];
        }
    }
    

    OR
    if you make selectArray function this way

    public function selectArray()
    {
        $args = func_get_args();
        $sql  = array_shift($args);
        $stmt  = $this->pdo->prepare($sql);
        $stmt->execute($args);
        return  $stmt->fetchAll();
    }
    

    you can save yourself a line or two:

    public function SelectStatus($user_id)
    {
        $query = "SELECT * FROM users U, posts P 
                      WHERE P.user_id_fk=U.user_id and U.user_id=?";
        return  $this->dbc->selectArray($query, $user_id);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键