Advertisement
Guest User

Untitled

a guest
Sep 9th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. <?php
  2. class database extends PDO{
  3.     private $host = DB_HOST;
  4.     private $user = DB_USER;
  5.     private $pass = DB_PASS;
  6.     private $dbname = DB_NAME;
  7.     private $dbh;
  8.     private $error;
  9.     private $stmt;
  10.    
  11.     public function __construct()
  12.     {
  13.         // Set DSN
  14.         $dsn = 'mysql:host=' . $this->host . ';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 any errors
  25.         catch (PDOException $e) {
  26.             echo $this->error = $e->getMessage();
  27.         }
  28.     }
  29.    
  30.     public function classConnectTest()
  31.     {
  32.         return "Connected";
  33.     }
  34.    
  35.     public function query($query){
  36.         $this->stmt = $this->dbh->prepare($query);
  37.     }
  38.    
  39.     public function bind($param, $value, $type = null){
  40.          if (is_null($type)) {
  41.             switch (true)
  42.             {
  43.                 case is_int($value):
  44.                   $type = PDO::PARAM_INT;  
  45.                   break;
  46.                 case is_bool($value):
  47.                   $type = PDO::PARAM_BOOL;
  48.                   break;
  49.                 case is_null($value):
  50.                   $type = PDO::PARAM_NULL;
  51.                   break;
  52.                 default:
  53.                   $type = PDO::PARAM_STR;
  54.             }
  55.             $this->stmt->bindValue($param, $value, $type);
  56.          }
  57.     }
  58.    
  59.     public function execute(){
  60.         return $this->stmt->execute();
  61.     }
  62.    
  63.     public function resultset(){
  64.         $this->execute();
  65.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  66.     }
  67.    
  68.     public function single(){
  69.         $this->execute();
  70.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  71.     }
  72.    
  73.     public function rowCount(){
  74.         return $this->stmt->rowCount();
  75.     }
  76.     /*
  77.     public function lastInsertId(){
  78.         return $this->dbh->lastInsertId();
  79.     }
  80.     */
  81.     public function beginTransaction(){
  82.         return $this->dbh->beginTransaction();
  83.     }
  84.    
  85.     public function endTransaction(){
  86.         return $this->dbh->commit();
  87.     }
  88.     public function cancelTransaction(){
  89.         return $this->dbh->rollBack();
  90.     }
  91.    
  92.     public function debugDumpParams(){
  93.         return $this->stmt->debugDumpParams();
  94.     }
  95.    
  96.     ##UPDATE CLASS
  97.     public function setTable($table){
  98.         $this->table = $table; 
  99.     }
  100.    
  101.     public function setWhere($where){
  102.         $this->where = $where; 
  103.     }
  104.    
  105.     public function dataWhere($isWhere){
  106.         $this->isWhere = $isWhere; 
  107.     }
  108.    
  109.     public function overwrite_update($row, $data){
  110.         if($row == 0 OR $data == 0){
  111.             $this->row = $row;
  112.             $this->data = $data;
  113.            
  114.             if(empty($this->where)){
  115.                 self::query("UPDATE `".$this->table."` SET `".$this->row."`=:row");
  116.                 self::bind(":row",$this->data);
  117.                 self::execute();
  118.                 return true;
  119.             }else{
  120.                 self::query("UPDATE `".$this->table."` SET `".$this->row."`=:row WHERE `".$this->where."`=:where");
  121.                 self::bind(":row",$this->data);
  122.                 self::bind(":where",$this->isWhere);
  123.                 self::execute();
  124.                 return true;
  125.             }
  126.         }else{
  127.             return false;  
  128.         }
  129.     }
  130.    
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement