Advertisement
irsyadul_ibad

Database.php

Nov 25th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. <?php
  2. class Database{
  3.     private $dbhost = DB_HOST;
  4.     private $dbtype = DB_TYPE;
  5.     private $dbname = DB_NAME;
  6.     private $user = DB_USER;
  7.     private $pass = DB_PASS;
  8.     private $dbh, $stmt;
  9.    
  10.     public function __construct(){
  11.         $dsn = "{$this->dbtype}:host={$this->dbhost};dbname={$this->dbname}";
  12.         //koneksi ke database
  13.         try{
  14.             $this->dbh = new PDO($dsn, $this->user, $this->pass);
  15.         }catch(PDOException $e){
  16.             die($e->getMessage());
  17.         }
  18.     }
  19.     public function query($query){
  20.         $this->stmt = $this->dbh->prepare($query);
  21.     }
  22.     public function bind($param, $value, $type= null){
  23.         if(is_null($type)){
  24.             switch(true){
  25.                 case is_int($value) :
  26.                     $type = PDO::PARAM_INT;
  27.                 break;
  28.                 case is_bool($value) :
  29.                     $type = PDO::PARAM_BOOL;
  30.                 break;
  31.                 case is_null($value) :
  32.                     $type = PDO::PARAM_NULL;
  33.                 break;
  34.                 default :
  35.                     $type = PDO::PARAM_STR;
  36.             }
  37.         }
  38.         $this->stmt->bindValue($param, $value, $type);
  39.     }
  40.     public function execute(){
  41.         $this->stmt->execute();
  42.     }
  43.     public function resultSet(){
  44.         $this->execute();
  45.         return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  46.     }
  47.     public function single(){
  48.         $this->execute();
  49.         return $this->stmt->fetch(PDO::FETCH_ASSOC);
  50.     }
  51.     public function rowCount(){
  52.         return $this->stmt->rowCount();
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement