Advertisement
MRYDesign

Bas - DB Object - Part 1

Oct 3rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1. <?php
  2. class Database extends PDO{
  3.     private $host = DB_HOST;
  4.     private $user = DB_USERNAME;
  5.     private $pass = DB_PASSWORD;
  6.     private $dbname = DB_NAME;
  7.     private $dbh;
  8.     private $error;
  9.     protected $stmt;
  10.    
  11.     public function __construct()
  12.     {
  13.         // Set DSN
  14.         $dsn = 'mysql:host=' . $this->host . ';port=8889;dbname=' . $this->dbname; 
  15.        
  16.         // Set options
  17.         $options = array(
  18.             PDO::ATTR_PERSISTENT => true,
  19.             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  20.         );
  21.        
  22.         try {
  23.             $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
  24.         }catch (PDOException $e) {
  25.             echo $this->error = $e->getMessage();
  26.         }
  27.     }
  28.    
  29.     public function query($query){
  30.         $this->stmt = $this->dbh->prepare($query);
  31.     }
  32.    
  33.     public function bind($param, $value, $type = null){
  34.        
  35.          if (is_null($type)) {
  36.             switch (true)
  37.             {
  38.                 case is_int($value):
  39.                   $type = PDO::PARAM_INT;  
  40.                   break;
  41.                 case is_bool($value):
  42.                   $type = PDO::PARAM_BOOL;
  43.                   break;
  44.                 case is_null($value):
  45.                   $type = PDO::PARAM_NULL;
  46.                   break;
  47.                 default:
  48.                   $type = PDO::PARAM_STR;
  49.             }
  50.             $this->stmt->bindValue($param, $value, $type);
  51.          }
  52.     }
  53.    
  54.     public function execute(){
  55.         return $this->stmt->execute();
  56.     }
  57.    
  58.     public function get_query(){
  59.         return $this->stmt;
  60.     }
  61.    
  62.     public function multiply(){
  63.         $this->execute();
  64.         return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  65.     }
  66.    
  67.     public function single(){
  68.         $this->execute();
  69.         return $this->stmt->fetch(PDO::FETCH_OBJ);
  70.     }
  71.    
  72.     public function fetch(){
  73.         $this->execute();
  74.         return $this->stmt->fetchAll(PDO::FETCH_OBJ);
  75.     }
  76.    
  77.     public function rowCount(){
  78.         return $this->stmt->rowCount();
  79.     }
  80.  
  81.     public function lastInsertId($segname = NULL){
  82.         return $this->dbh->lastInsertId();
  83.     }
  84.  
  85.     public function beginTransaction(){
  86.         return $this->dbh->beginTransaction();
  87.     }
  88.    
  89.     public function endTransaction(){
  90.         return $this->dbh->commit();
  91.     }
  92.     public function cancelTransaction(){
  93.         return $this->dbh->rollBack();
  94.     }
  95.    
  96.     public function debugDumpParams(){
  97.         return $this->stmt->debugDumpParams();
  98.     }
  99.    
  100.     ##UPDATE CLASS
  101.     public function setTable($table){
  102.         $this->table = $table; 
  103.     }
  104.    
  105.     public function setWhere($where){
  106.         $this->where = $where; 
  107.     }
  108.    
  109.     public function dataWhere($isWhere){
  110.         $this->isWhere = $isWhere; 
  111.     }
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement