Advertisement
Guest User

database

a guest
Apr 22nd, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * @author  Elvis Halilovic
  5.  * @copyright   Elvis Halilovic
  6.  * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  7.  * @package core\system\database
  8.  */
  9.  
  10. namespace core\system\database;
  11.  
  12. class database {
  13.    
  14.     private $DB,
  15.             $STMT,
  16.             $OPTIONS = array(\PDO::ATTR_PERSISTENT => true, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION),
  17.             $HOST,
  18.             $USER,
  19.             $PASS,
  20.             $DATA;
  21.    
  22.     public  $RESULT = null,
  23.             $INSTANCE;
  24.  
  25.     public function __construct() {
  26.         $this->HOST = \HMConfig::DATABASE_HOST;
  27.         $this->USER = \HMConfig::DATABASE_USER;
  28.         $this->PASS = \HMConfig::DATABASE_PASS;
  29.         $this->DATA = \HMConfig::DATABASE_NAME;
  30.         $this->connect();
  31.     }
  32.  
  33.     private function connect() {
  34.         try {
  35.             $this->DB = new \PDO("mysql:host={$this->HOST};dbname={$this->DATA}", $this->USER, $this->PASS, $this->OPTIONS);
  36.         } catch (\PDOException $ex) {
  37.             echo $ex->getMessage();
  38.         }
  39.         return $this;
  40.     }
  41.  
  42.     public function query($query){
  43.         $this->STMT = $this->DB->prepare($query);
  44.     }
  45.  
  46.     public function bind($param, $value, $type = null){
  47.         if(is_null($type)){
  48.             switch(true){
  49.                 case is_int($value):
  50.                     $type = \PDO::PARAM_INT;
  51.                     break;
  52.                 case is_bool($value):
  53.                     $type = \PDO::PARAM_BOOL;
  54.                     break;
  55.                 case is_null($value):
  56.                     $type = \PDO::PARAM_NULL;
  57.                     break;
  58.                 default:
  59.                     $type = \PDO::PARAM_STR;
  60.             }
  61.         }
  62.         $this->STMT->bindValue($param, $value, $type);
  63.     }
  64.  
  65.     public function bindArray($array) {
  66.         foreach($array as $key => $value) {
  67.             $this->bind($key, $value);
  68.         }
  69.         return true;
  70.     }
  71.  
  72.     public function execute() {
  73.         return $this->STMT->execute();
  74.     }
  75.  
  76.     public function getAllData() {
  77.         $this->execute();
  78.         return $this->STMT->fetchAll(\PDO::FETCH_ASSOC);
  79.     }
  80.  
  81.     public function getData() {
  82.         $this->execute();
  83.         return $this->STMT->fetch(\PDO::FETCH_ASSOC);
  84.     }
  85.  
  86.     public function rowCount() {
  87.         return $this->STMT->rowCount();
  88.     }
  89.  
  90.     public function lastInsertId() {
  91.         return $this->DB->lastInsertId();
  92.     }
  93.  
  94.     public function beginTransaction() {
  95.         return $this->DB->beginTransaction();
  96.     }
  97.  
  98.     public function endTransaction() {
  99.         return $this->DB->commit();
  100.     }
  101.  
  102.     public function cancelTransaction() {
  103.         return $this->DB->rollback();
  104.     }
  105.  
  106.     public function debugDumpParams() {
  107.         return $this->STMT->debugDumpParams();
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement