Advertisement
MRYDesign

PHP OOP Class Database

Oct 6th, 2014
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. class database extends PDO{
  2.     private $host = DB_HOST;
  3.     private $user = DB_USER;
  4.     private $pass = DB_PASS;
  5.     private $dbname = DB_NAME;
  6.     private $dbh;
  7.     private $error;
  8.     private $stmt;
  9.    
  10.     public function __construct()
  11.     {
  12.         // Set DSN
  13.         $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;   
  14.        
  15.         // Set options
  16.         $options = array(
  17.             PDO::ATTR_PERSISTENT => true,
  18.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  19.         );
  20.        
  21.         try {
  22.             $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  23.         }// Catch any errors
  24.         catch (PDOException $e) {
  25.             return $this->error = $e->getMessage();
  26.         }
  27.     }
  28.    
  29.     public function classConnectTest()
  30.     {
  31.         return "Connected";
  32.     }
  33.    
  34.     public function query($query){
  35.         $this->stmt = $this->dbh->prepare($query);
  36.     }
  37.    
  38.     public function bind($param, $value, $type = null){
  39.          if (is_null($type)) {
  40.             switch (true)
  41.             {
  42.                 case is_int($value):
  43.                   $type = PDO::PARAM_INT;  
  44.                   break;
  45.                 case is_bool($value):
  46.                   $type = PDO::PARAM_BOOL;
  47.                   break;
  48.                 case is_null($value):
  49.                   $type = PDO::PARAM_NULL;
  50.                   break;
  51.                 default:
  52.                   $type = PDO::PARAM_STR;
  53.             }
  54.             $this->stmt->bindValue($param, $value, $type);
  55.          }
  56.     }
  57.    
  58.     public function execute(){
  59.         return $this->stmt->execute();
  60.     }
  61.    
  62.     public function mutltiply(){
  63.         $this->execute();
  64.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  65.     }
  66.    
  67.     public function single(){
  68.         $this->execute();
  69.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  70.     }
  71.    
  72.     public function rowCount(){
  73.         return $this->stmt->rowCount();
  74.     }
  75.    
  76.     public function lastInsertId(){
  77.         return $this->dbh->lastInsertId();
  78.     }
  79.  
  80.     public function beginTransaction(){
  81.         return $this->dbh->beginTransaction();
  82.     }
  83.    
  84.     public function endTransaction(){
  85.         return $this->dbh->commit();
  86.     }
  87.     public function cancelTransaction(){
  88.         return $this->dbh->rollBack();
  89.     }
  90.    
  91.     public function debugDumpParams(){
  92.         return $this->stmt->debugDumpParams();
  93.     }
  94.    
  95.    
  96.    
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement