Advertisement
Guest User

dbmvc-2

a guest
Jan 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.65 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 $db_name = DB_NAME;
  8.  
  9.    private $dbh;
  10.    private $stmt;
  11.  
  12.     public function __construct()
  13.     {
  14.         // data source name
  15.         $dsn = 'mysql:host='.$this->host.';dbname='.$this->db_name;
  16.         $option = [
  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, $option);
  23.         } catch(PDOException $e){
  24.             die($e->getMessage());
  25.         }
  26.     }
  27.  
  28.  
  29.      public function query($query)
  30.     {
  31.         $this->stmt = $this->dbh->prepare($query);
  32.     }
  33.  
  34.     public function bind($param, $value, $type = null)
  35.     {
  36.         if( is_null($type) ){
  37.             switch( true ){
  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.         }
  51.         $this->stmt->bindValue($param, $value, $type);
  52.     }
  53.  
  54.     public function execute()
  55.     {
  56.         $this->stmt->execute();
  57.     }
  58.  
  59.     public function resultSet()
  60.     {
  61.         $this->execute();
  62.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  63.     }
  64.  
  65.     public function single()
  66.     {
  67.         $this->execute();
  68.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  69.     }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement