我的php pdo类需要太多时间来执行

时间:2017-08-20 06:27:49

标签: php mysql pdo

我用pdo创建了一个php数据库类来使用我的项目。它非常简单。但我注意到一件事,执行需要太多时间。你能告诉我我哪里做错了吗?

这是我的数据库类代码

class database{


    public $error;

    private $db;
    private $dbhost = 'localhost';
    private $dbname = 'support_database';
    private $dbuser = 'root';
    private $dbpass = '';
    public $site_url = "http://localhost/support";

    public function connect(){

        try {
            $this->db = new PDO("mysql:host={$this->dbhost};dbname={$this->dbname}",$this->dbuser,$this->dbpass);
            $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }

        catch(PDOException $e) {
           // echo "Connection error: ".$e->getMessage();
        }
    }

    public function deleteRecord($id){
        $this->Connect();
        $statement = $this->db->prepare("delete from officers_table where id=?");
        $statement->execute(array($id));
    }

    public function showRecord($tablename){
        $this->Connect();
        $statement = $this->db->prepare("select * from $tablename");
        $statement->execute();
        return $result = $statement->fetchAll(PDO::FETCH_ASSOC);


    }

    public function addRecord($tablename,$fields,$values){
        $this->Connect();

        $items = $fields;
        $id = count($items);
        $newarray = array();
        for($i = 0; $i<$id; $i++){
            $newarray[] = "?";
        }

        $nval = implode(",",$newarray);

        $fields = implode(",",$fields);

        $statement = $this->db->prepare("insert into $tablename ($fields) values($nval)");
        $statement->execute($values);

        return true;
    }

    public function addUser($tablename,$fields,$values){
        $this->Connect();

        $items = $fields;
        $id = count($items);
        $newarray = array();
        for($i = 0; $i<$id; $i++){
            $newarray[] = "?";
        }

        $nval = implode(",",$newarray);

        $fields = implode(",",$fields);

        $statement = $this->db->prepare("insert into $tablename ($fields) values($nval)");
        $statement->execute($values);

        return true;
    }

    public function checkUser($tablename,$field,$value){
        $this->Connect();

        $num = 0;
        $statement = $this->db->prepare("select * from $tablename where $field=?");
        $statement->execute(array($value));     

        $num = $statement->rowCount();

        if($num>0) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public function SingleValueSearch($tablename,$field,$value,$id="id",$ordertype="DESC"){
        $this->Connect();

        $statement = $this->db->prepare("select * from $tablename where $field=? order by $id $ordertype");
        $statement->execute(array($value));     

        return $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    public function allTicket($tablename){
        $this->Connect();

        $statement = $this->db->prepare("select * from $tablename order by id DESC");
        $statement->execute();      

        return $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    public function userTicket($tablename,$field,$value,$id="id",$ordertype="DESC"){
        $this->Connect();


        $statement = $this->db->prepare("select * from $tablename where $field=? order by $id $ordertype");
        $statement->execute(array($value));     

        return $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    public function currentUserId($email){
        $this->Connect();

        $statement = $this->db->prepare("select * from support_users where email=?");
        $statement->execute(array($email));     

        return $result = $statement->fetchAll(PDO::FETCH_ASSOC);
    }

    public function lastId($tablename){
        $this->Connect();
        $statement = $this->db->prepare("select * from $tablename order by id DESC LIMIT 1");
        $statement->execute();
        return $result = $statement->fetchAll(PDO::FETCH_ASSOC);


    }


    public function emptyCheck($tablename,$user_id,$user_role){
        $this->Connect();

        $num = 0;

        if($user_role == 'admin'){
            $statement = $this->db->prepare("select * from $tablename");
            $statement->execute();      

            $num = $statement->rowCount();

            if($num>0){
                return true;
            }
            else
            {
                return false;
            }

        }
        else{

            $statement = $this->db->prepare("select * from $tablename where user_id =?");
            $statement->execute(array($user_id));       

            $num = $statement->rowCount();

            if($num>0){
                return true;
            }
            else
            {
                return false;
            }
        }


    }

    public function fileCheck($name) {
      foreach ($_FILES[$name]['error'] as $ferror) {
        if ($ferror != UPLOAD_ERR_NO_FILE) {
          return true;
        }
      }
      return false;
    }

    public function deleteTicket($id){
        $this->Connect();

        $statement = $this->db->prepare("select * from support_tickets where id=?");
        $statement->execute(array($id));
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);
        $media_id = unserialize($result[0]['media_id']);

        foreach( $media_id as $mid ){

            $statement = $this->db->prepare("delete from support_media where id=?");
            $statement->execute(array($mid));

        }

        $statement = $this->db->prepare("delete from support_tickets where id=?");
        $statement->execute(array($id));

        return true;
    }

}

在我的项目中,我使用的是这样的东西。以下链接的代码需要很长时间才能执行。我在此页面上仅使用了更多查询。

https://pastebin.com/0UhETNgG

请帮助我找出我犯的错误。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您可以在现有代码中进行一些优化。

数据库连接

您目前正在为数据库的每个查询创建新连接。这很昂贵,所以你应该重用当前的连接。

connect()方法的开头添加:

public function connect(){
    if ($this->db) {
        // There already is a connection, return it instead
        return $this->db;
    }

    // Your current try/catch block
}

一遍又一遍地重复相同的查询

您还在同一个脚本中反复从数据库中提取相同的记录。即使DB调用很快(假设db已正确索引),它仍然是不必要的开销。您应该一次获取它,将其存储在变量中并重用变量:

$ticket = $db->SingleValueSearch('support_tickets','id',$_GET['id'],'i‌​d','DESC');

现在,您可以替换所有相同的调用,而不是使用变量$ticket

答案 1 :(得分:-1)

根据Magnus Eriksson's说明我做了更改我的代码。现在它像火箭一样工作:)非常感谢Sir先生。

这是我的Connect方法的代码。我刚刚用if条件改变了。

public function connect(){

        try {
            if($this->db){
                return $this->db;
            }
            else{
                $this->db = new PDO("mysql:host={$this->dbhost};dbname={$this->dbname}",$this->dbuser,$this->dbpass);
                $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }

        }

        catch(PDOException $e) {
           // echo "Connection error: ".$e->getMessage();
        }
    }