Advertisement
Guest User

OOP

a guest
Mar 22nd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.14 KB | None | 0 0
  1. <?php
  2. defined("BASEPATH") or die("Access Denied");
  3. /**
  4.  * Database
  5.  */
  6. class DB
  7. {
  8.    public $connect;
  9.    public $host;
  10.    public $user;
  11.    public $pass;
  12.    public $database;
  13.    public function __construct()
  14.    {
  15.       $this->host = "localhost";
  16.       $this->user ="root";
  17.       $this->pass = "";
  18.       $this->database = "lks";
  19.       $this->connect = mysqli_connect($this->host,$this->user,$this->pass,$this->database) or die(mysqli_errno());
  20.    }
  21.    public function query($data){
  22.       return mysqli_query($this->connect,$data) or die(mysqli_error($connect));
  23.    }
  24.    public function select($table,$num=null){
  25.       $query = "SELECT * FROM ".$table;
  26.       if($num){
  27.          return $this->num($query);
  28.       }
  29.       return $this->fetch($query);
  30.    }
  31.    public function insert($data,$table){
  32.       $query = "INSERT INTO ";
  33.       if(is_array($data)){
  34.          $value = "'".implode("','",$data)."'";
  35.          $key = "'".implode("','",array_keys($data))."'";
  36.          $query .= "(".$key.") VALUES (".$key.")";
  37.       }
  38.       else {
  39.          $query = "VALUES(".$data.")";
  40.       }
  41.       $query .= " FROM ".$table;
  42.       return $this->query($query);
  43.    }
  44.    public function delete($table,$where){
  45.       $query = "DELETE FROM ".$table." WHERE ";
  46.       if(is_array($where)){
  47.          $i = 0;
  48.          foreach ($where as $key=>$value) {
  49.             $query .= "'".$key."'='".$value."'";
  50.             if(count($where)-1 != $i){
  51.                $query .= ",";
  52.             }
  53.             $i++;
  54.          }
  55.       }
  56.       else {
  57.          $query .= $where;
  58.       }
  59.       return $this->query($query);
  60.    }
  61.    public function update($data,$table,$where){
  62.       $query = "UPDATE ".$table." SET ";
  63.       if(is_array($data)){
  64.          $i = 0;
  65.          foreach ($data as $key=>$value) {
  66.             $query .= "'".$key."'='".$value."'";
  67.             if(count($data)-1 != $i){
  68.                $query .= ",";
  69.             }
  70.             $i++;
  71.          }
  72.       }
  73.       else {
  74.          $query .= $data;
  75.       }
  76.       $query .= " WHERE ";
  77.       if(is_array($where)){
  78.          $i = 0;
  79.          foreach ($where as $key=>$value) {
  80.             $query .= "'".$key."'='".$value."'";
  81.             if(count($where)-1 != $i){
  82.                $query .= ",";
  83.             }
  84.             $i++;
  85.          }
  86.       }
  87.       else {
  88.          $query .= $where;
  89.       }
  90.       return $this->query($query);
  91.    }
  92.    public function fetch($data,$type=null,$type2=MYSQLI_BOTH){
  93.       $data = mysqli_query($this->connect,$data) or die(mysqli_error($connect));
  94.       if($type != null){
  95.          return mysqli_fetch_array($data,$type2);
  96.       }
  97.       else {
  98.          return mysqli_fetch_object($data);
  99.       }
  100.    }
  101.    public function num($data){
  102.       $data = mysqli_query($this->connect,$data) or die(mysqli_error($connect));
  103.       return mysqli_num_rows($data);
  104.    }
  105. }
  106. $db = new DB;
  107. $data = [
  108.    "id_user"=>'',
  109.    "username" =>"ppabcd",
  110.    "password" =>md5("q1w2e3r4t5"),
  111.    "nama" =>"Reza Juliandri",
  112.    "email" =>"asdasd",
  113.    "token" =>"asdasd",
  114.    "creation_date" =>"asdasd",
  115.    "last_login" =>"asdasd"
  116. ];
  117. $result = $db->insert($data,"user");
  118. var_dump($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement