Do I have this PDO connection class?

I have been working with PDO for the past few days, I am working on a small CMS system to teach OOP skills, but although this is only a small CMS, I want it to be able to handle everything the web can throw at it.

This is where I came to, I'm going to add a connection pool to the constructor to provide the possibility of a large number of parallel connections on demand. I am very new to this OOP material, so I want to advise and criticize a little, no doubt I did something terrible here.

Did I accept the main Global or Singleton answer to connect to the database? as a basic design, although I added a private constructor, since I want to use $ this → dbConnectionInstance in the whole class to use additional helper functions.

Thank you very much for your time, I will really appreciate any advice you can give me,

-Drew

// Usage Example: $dbconn = dbManager::getConnection();
//                $dbconn->query("SELECT * FROM accounts WHERE id=:id", "':id' => $id");

<?php

class dbManager {
    private static $dbManagerInstance;
    private $dbConnectionInstance;
    private $stime;
    private $etime;
    public $timespent;
    public $numqueries;
    public $queries = array();

    public static function getManager(){
        if (!self::$dbManagerInstance){
            self::$dbManagerInstance = new dbManager();
        }
        return self::$dbManagerInstance;
    }

    // Server details stored in definition file
    private function __construct($db_server=DB_SERVER, $db_user=DB_USER, $db_pass=DB_PASS, $db_params=array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")) {
        if(!$this->dbConnectionInstance)
        {
            try{
                $this->dbConnectionInstance = new PDO($db_server, $db_user, $db_pass, $db_params);
                $this->dbConnectionInstance->setAttribute(PDO::ATTR_PERSISTENT, PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            } catch (PDOException $e) {
                $this->dbConnectionInstance = null;
                die($e->getMessage());
            }
        }
        return $this->dbConnectionInstance;
    }

    private function __destruct(){
        $this->dbConnectionInstance = null;
    }

    private function query($sql, $params = array()) {
        $this->queries[] = $sql;
        $this->numqueries++;
        $this->sTime = microtime();
        $stmt = $this->dbConnectionInstance->prepare($sql);
        $stmt->execute($params);
        $this->eTime = microtime();
        $this->timespent += round($this->eTime - $this->sTime, 4);
        return $stmt;
    }

}

?>

Thank you for your suggestions, now I added a rollback and fixed exceptions in my handling, I'm just studying the use of buffered requests, I'm not quite sure what they will give me?

+3
source share
2 answers

, rollback, / ( ):

try {
    $this->dbConnectionInstance->beginTransaction();
    $stmt = $this->dbConnectionInstance->prepare($sql);
    $stmt->execute($params);
    $this->dbConnectionInstance->commit();
}catch(PDOException $e){
    $this->dbConnectionInstance->rollback();
}

commit(), beginTransaction()

EDIT: :

+3

, , . , ( ).

false . $this->dbConnectionInstance->errorInfo() .

, , : PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true

. CMS.

0

Source: https://habr.com/ru/post/1721181/


All Articles