0

我正在使用 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;
            }
        }
    }
}
?>

但遗憾的是,我总是收到错误 ->致命错误:第 14 行的 C:\xampp\htdocs\RIO\RIO\RAI\session_rai\includes\db.php 中的最大执行时间超过 30 秒

那么,这种情况有什么解决方案吗?谢谢。

4

2 回答 2

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";
于 2013-04-27T11:19:46.033 回答
0

你的班级结构是错误的。

  1. 你不需要 DBConnector 类
  2. 永远不要使用相同的凭据创建多个到数据库的连接
  3. MyWeb 类中的大部分代码也是无用的

因此,创建一个 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'];
    }
}

或者
,如果您以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);
}
于 2013-04-27T11:44:22.493 回答