Guest User

Untitled

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