Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. <?php
  2.  
  3. class db {
  4.  
  5.     protected $db;
  6.     private $db_host = "localhost";
  7.     private $db_user = "root";
  8.     private $db_pass = "root";
  9.     private $db_name = "mvc";
  10.     private $sql;
  11.     private $stmt;
  12.     private $result;
  13.     private $row = array();
  14.  
  15.     private function __construct() {
  16.        
  17.     }
  18.  
  19.     public function connect_error() {
  20.         echo "DB ERROR. If issue persist contact system Admin";
  21.         exit();
  22.     }
  23.  
  24.     public function error() {
  25.         echo "Error has with the DB";
  26.         exit();
  27.     }
  28.  
  29.     public function _connect() {
  30.         $this->db = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
  31.         if (mysqli_connect_errno()) {
  32.             $this->connect_error();
  33.         }
  34.  
  35.         mysqli_set_chartset($this->db, "utf8");
  36.     }
  37.  
  38.     /**
  39.      * Insert into DB
  40.      */
  41.     public function query($sql, $params = NULL, $sanitize = TRUE) {
  42.  
  43.         $this->$sql = $this->sanitize($sql, $sanitize);
  44.  
  45.         if (!$this->stmt = $this->db->prepare($this->sql)) {
  46.             $this->error();
  47.             return false;
  48.         }
  49.  
  50.         if (is_array($params)) {
  51.             $this->bindparams($params);
  52.         }
  53.  
  54.         if (!$this->stmt->execute()) {
  55.             $this->error();
  56.         }
  57.  
  58.         $this->stmt->reset();
  59.         $this->stmt->close();
  60.     }
  61.  
  62.     public function fetch_val($sql, $bindparams = NULL, $sanitize = TRUE) {
  63.         $this->$sql = $this->sanitize($sql);
  64.  
  65.         if ($this->sql = $this->db->prepare($this->sql) != TRUE) {
  66.             $this->error();
  67.         }
  68.        
  69.         if ($this->stmt->execute() != TRUE) {
  70.             $this->error();
  71.             exit();
  72.         } else {
  73.             $this->result = $this->stmt->get_result();
  74.             return $this->result;
  75.         }
  76.  
  77.  
  78.         $this->stmt->free_result();
  79.         $this->stmt->close();
  80.     }
  81.  
  82.     protected function bindparams($arrParams) {
  83.         $params = array();
  84.         $params[0] = "";
  85.  
  86.         foreach ($arrParams as $key => $value) {
  87.             $params[0] .= $this->gettype($value);
  88.             array_push($params, $arrParams[$key]);
  89.         }
  90.  
  91.         call_user_func_array(array($this->stmt, 'bind_param'), $params);
  92.     }
  93.  
  94.     /**
  95.      * returns type of value
  96.      */
  97.     protected function gettype($var) {
  98.         switch (gettype($var)) {
  99.             case 'NULL':
  100.             case 'string':
  101.                 return 's';
  102.                 break;
  103.             case 'boolean':
  104.             case 'interger':
  105.                 return 'i';
  106.                 break;
  107.             case 'blob':
  108.                 return 'b';
  109.                 break;
  110.             case 'double':
  111.                 return 'd';
  112.                 break;
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * sanitize the value
  118.      */
  119.     protected function sanitize($sql) {
  120.         $str_sanitized = ($sanitize) ? filter_var($sql, FILTER_SANITIZE_STRING, FITLER_FLAG_NO_ENCODE_QUOTES) : $sql;
  121.         return $str_sanitized;
  122.     }
  123.  
  124.     /**
  125.      * close db connection
  126.      */
  127.     public function _close() {
  128.         $this->db->close();
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement