Forked from Badshah1/php OOP mysqliDatabaseclass.php
Created
August 10, 2020 20:02
-
-
Save dhrubapuc23/3c5ccea25c43e2ea7e075347d44187c2 to your computer and use it in GitHub Desktop.
php oop mysqli database class insert,update,delete,select methods are there in given file if you have easy way then please share your methods thanks alot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database{ | |
private $host="localhost"; | |
private $username="root"; | |
private $password="mysql"; | |
private $database="sale"; | |
private $DbCon; | |
public function connect(){ | |
$con = new mysqli($this->host,$this->username,$this->password,$this->database); | |
if($con){ | |
$this->DbCon=$con; | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
public function select($table,$row="*",$where=null,$order=null){ | |
$query='SELECT '.$row.' FROM '.$table; | |
if($where!=null){ | |
$query.=' WHERE '.$where; | |
} | |
if($order!=null){ | |
$query.=' ORDER BY '; | |
} | |
$Result=$this->DbCon->query($query); | |
return $Result; | |
} | |
public function insert($table,$value,$row=null){ | |
$insert= " INSERT INTO ".$table; | |
if($row!=null){ | |
$insert.=" (". $row." ) "; | |
} | |
for($i=0; $i<count($value); $i++){ | |
if(is_string($value[$i])){ | |
$value[$i]= '"'. $value[$i] . '"'; | |
} | |
} | |
$value=implode(',',$value); | |
$insert.=' VALUES ('.$value.')'; | |
$ins=$this->DbCon->query($insert); | |
if($ins){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
public function delete($table,$where=null){ | |
if($where == null) | |
{ | |
$delete = "DELETE ".$table; | |
} | |
else | |
{ | |
$delete = "DELETE FROM ".$table." WHERE ".$where; | |
} | |
$del=$this->DbCon->query($delete); | |
if($del){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
public function update($table,$rows,$where){ | |
// Parse the where values | |
// even values (including 0) contain the where rows | |
// odd values contain the clauses for the row | |
for($i = 0; $i < count($where); $i++) | |
{ | |
if($i%2 != 0) | |
{ | |
if(is_string($where[$i])) | |
{ | |
if(($i+1) != null) | |
$where[$i] = '"'.$where[$i].'" AND '; | |
else | |
$where[$i] = '"'.$where[$i].'"'; | |
} | |
} | |
} | |
$where = implode(" ",$where); | |
$update = 'UPDATE '.$table.' SET '; | |
$keys = array_keys($rows); | |
for($i = 0; $i < count($rows); $i++) | |
{ | |
if(is_string($rows[$keys[$i]])) | |
{ | |
$update .= $keys[$i].'="'.$rows[$keys[$i]].'"'; | |
} | |
else | |
{ | |
$update .= $keys[$i].'='.$rows[$keys[$i]]; | |
} | |
// Parse to add commas | |
if($i != count($rows)-1) | |
{ | |
$update .= ','; | |
} | |
} | |
$update .= ' WHERE '.$where; | |
$query = $this->DbCon->query($update); | |
if($query) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
}; | |
$a= new Database(); | |
$a->connect(); | |
$upd=array('username'=>'Badshah', | |
'password'=>'badshah', | |
'email'=>'[email protected]'); | |
$a->update('user',$upd,array('id=3','id=4','id=5','id=6')); | |
//$a->delete('user',' id = 1'); | |
//$ins=array('','Badshah','badshah','[email protected]'); | |
//$a->insert('user',$ins,null); | |
//$ab=$a->select('user'); | |
//while($a=$ab->fetch_array()){ | |
// echo $a[0]."<br />"; | |
//} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment