Advertisement
leo8agustino

Database

Dec 13th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.35 KB | None | 0 0
  1. <?php
  2. class Database{
  3.     private static $INSTANCE = null;
  4.     private $con,
  5.     $HOST = 'localhost',
  6.     $USER = 'root',
  7.     $PASS = '',
  8.     $DBNAME = 'dbpegawai';
  9.    
  10.     public function __construct(){
  11.         $this->con = new mysqli($this->HOST, $this->USER, $this->PASS, $this->DBNAME);
  12.         if(mysqli_connect_error()){
  13.             die ('Tidak terhubung ke database');
  14.         }
  15.     }
  16.     /*singleton pattern, menguju koneksi agar tidak double */
  17.     public static function getInstance(){
  18.         if(!isset(self::$INSTANCE)){
  19.             self::$INSTANCE = new Database();
  20.         }
  21.         return self::$INSTANCE;
  22.     }
  23.     public function insert($table, $fields = array()){
  24.         //mengambil column
  25.         $column = implode(", ", array_keys($fields));
  26.        
  27.         //mengambil nilai
  28.         $valueArrays = array();
  29.         $i = 0;
  30.         foreach($fields as $key=>$values) {
  31.             if(is_int($values)){
  32.                 $valueArrays[$i] = $this->escapeInput($values);
  33.             }else{
  34.                 $valueArrays[$i] = "'".$this->escapeInput($values)."'";
  35.             }
  36.             $i++;
  37.         }
  38.         $values = implode(", ", $valueArrays);
  39.         $query = "INSERT INTO $table($column) Values($values)";
  40.        
  41.         return $this->run_query($query, 'Insert data gagal');
  42.     }
  43.     public function run_query($query, $msg) {
  44.         if($this->con->query($query))
  45.             return true;
  46.         else
  47.             die($msg);
  48.     }
  49.     public function escapeInput($name) {
  50.         return $this->con->real_escape_string($name);
  51.     }
  52.         public function getInfo($table, $column='', $value='') {
  53.             if(!is_int($value)){
  54.                 $value = "'".$value."'";
  55.             }
  56.             if($column != '') {
  57.             $query = "SELECT * FROM $table WHERE $column = $value";
  58.             $result = $this->con->query($query);
  59.                
  60.             while ($row = $result->fetch_assoc()){
  61.                 return $row;
  62.         }
  63.     }else{
  64.         $query = "SELECT * FROM $table";
  65.         $result = $this->con->query($query);
  66.         while($row = $result->fetch_assoc()) {
  67.             $result[] = $row;
  68.         }
  69.         return $result;
  70.     }
  71.         }
  72.     public function update($table, $field, $id) {
  73.         //mengambil nilai
  74.         $valueArrays = array();
  75.         $i = 0;
  76.         foreach($field as $key=>$values) {
  77.             //update table set kunci1=nilai, kunci2=nilai...
  78.             if(is_int($values)){
  79.                 $valueArrays[$i]= $key ."=". $this->escapeInput($values);
  80.             }else{
  81.                 $valueArrays[$i]= $key ."='". $this->escapeInput($values)."'";
  82.             }
  83.             $i++;
  84.         }
  85.         $values = implode(", ", $valueArrays);
  86.         $query = "UPDATE $table SET $values WHERE id = $id";
  87.         // die($quiery);
  88.         return $this->run_query($query, 'ada masalah update');
  89.            
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement