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条)

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码