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

时间:2013-04-27 11:03:09

标签: php mysql pdo

我使用PDO连接数据库并在编码中使用OOP方法

这是如何获取帖子和评论

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

以及如何调用这些功能并显示帖子和评论

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

但遗憾的是我总是得到错误 - &gt; 致命错误:第14行的C:\ xampp \ htdocs \ RIO \ RIO \ RAI \ session_rai \ includes \ db.php超出了30秒的最长执行时间

那么,这种情况的解决方案是什么? 感谢。

2 个答案:

答案 0 :(得分:0)

您的语法错误

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

虽然应该

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

答案 1 :(得分:0)

你的班级结构错了。

  1. 您不需要DBConnector类
  2. 永远不要创建多个具有相同凭据的数据库连接
  3. MyWeb类中的大多数代码也没用
  4. 因此,创建PDO连接,然后实例化MyWeb类,然后获取数据

    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();
        }
    }    
    

    与输出相同

    <?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
    如果你使selectArray以这种方式运作

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

    你可以为自己保留一两行:

    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);
    }