Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined("BASEPATH") or die("Access Denied");
- /**
- * Database
- */
- class DB
- {
- public $connect;
- public $host;
- public $user;
- public $pass;
- public $database;
- public function __construct()
- {
- $this->host = "localhost";
- $this->user ="root";
- $this->pass = "";
- $this->database = "lks";
- $this->connect = mysqli_connect($this->host,$this->user,$this->pass,$this->database) or die(mysqli_errno());
- }
- public function query($data){
- return mysqli_query($this->connect,$data) or die(mysqli_error($connect));
- }
- public function select($table,$num=null){
- $query = "SELECT * FROM ".$table;
- if($num){
- return $this->num($query);
- }
- return $this->fetch($query);
- }
- public function insert($data,$table){
- $query = "INSERT INTO ";
- if(is_array($data)){
- $value = "'".implode("','",$data)."'";
- $key = "'".implode("','",array_keys($data))."'";
- $query .= "(".$key.") VALUES (".$key.")";
- }
- else {
- $query = "VALUES(".$data.")";
- }
- $query .= " FROM ".$table;
- return $this->query($query);
- }
- public function delete($table,$where){
- $query = "DELETE FROM ".$table." WHERE ";
- if(is_array($where)){
- $i = 0;
- foreach ($where as $key=>$value) {
- $query .= "'".$key."'='".$value."'";
- if(count($where)-1 != $i){
- $query .= ",";
- }
- $i++;
- }
- }
- else {
- $query .= $where;
- }
- return $this->query($query);
- }
- public function update($data,$table,$where){
- $query = "UPDATE ".$table." SET ";
- if(is_array($data)){
- $i = 0;
- foreach ($data as $key=>$value) {
- $query .= "'".$key."'='".$value."'";
- if(count($data)-1 != $i){
- $query .= ",";
- }
- $i++;
- }
- }
- else {
- $query .= $data;
- }
- $query .= " WHERE ";
- if(is_array($where)){
- $i = 0;
- foreach ($where as $key=>$value) {
- $query .= "'".$key."'='".$value."'";
- if(count($where)-1 != $i){
- $query .= ",";
- }
- $i++;
- }
- }
- else {
- $query .= $where;
- }
- return $this->query($query);
- }
- public function fetch($data,$type=null,$type2=MYSQLI_BOTH){
- $data = mysqli_query($this->connect,$data) or die(mysqli_error($connect));
- if($type != null){
- return mysqli_fetch_array($data,$type2);
- }
- else {
- return mysqli_fetch_object($data);
- }
- }
- public function num($data){
- $data = mysqli_query($this->connect,$data) or die(mysqli_error($connect));
- return mysqli_num_rows($data);
- }
- }
- $db = new DB;
- $data = [
- "id_user"=>'',
- "username" =>"ppabcd",
- "password" =>md5("q1w2e3r4t5"),
- "nama" =>"Reza Juliandri",
- "email" =>"asdasd",
- "token" =>"asdasd",
- "creation_date" =>"asdasd",
- "last_login" =>"asdasd"
- ];
- $result = $db->insert($data,"user");
- var_dump($result);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement