The following is a small class I extended MySQLi with. I'm going to use this in my upcoming projects, but my main reason to do this class is learning, so I would like to submit this for your review, tell me if there are any bugs or improvements to be made.
NOTES:
I know about prepared statements, but I am not going to only run the same queries with different parameters, I'm expecting very different queries in each page, So I don't want to use prepared statements. Don't suggest prepared statements.
I am aware of PDO, but I will use MySQL only, so I'm better with MySQLi. So please don't suggest PDO.
class Dbquery extends MySQLi
{
public $host,$user,$password,$database,$connection;
public function __construct($host,$user,$password,$database)
{
$this -> host = $host;
$this -> user = $user;
$this -> password = $password;
$this -> database = $database;
$this -> connect_me();
}
private function connect_me()
{
$this -> connection = $this-> connect($this->host,$this->user,$this->password,$this->database);
if( $this -> connect_error )
die($this->connect_error);
}
private function extracts($data) // function used to append data and column names for insert query
{
$column = array("","");
foreach ($data as $index => $details)
{
$column[0].= $index.",";
$column[1].= "'".$this ->real_escape_string($details)."',";
}
$column[0] = rtrim($column[0],",");
$column[1] = rtrim($column[1],",");
return $column;
}
private function append($data) // function used to append data and column names for update query
{
$string = "";
foreach($data as $index => $details)
{
$string.= $index."='".$this -> real_escape_string($details)."',";
}
$string = rtrim($string,",");
return $string;
}
public function insert($table,$data)
{
$extracted = $this -> extracts($data);
$this -> query("INSERT INTO ".$table." (".$extracted[0].") VALUES(".$extracted[1].")");
if($this -> error)
return $this -> error;
else
return $this -> insert_id;
}
public function update($table,$data,$where)
{
$extracted = $this -> append($data);
$this -> query("UPDATE ".$table." SET ".$extracted." WHERE ".$where);
if($this -> error)
return $this -> error;
}
public function select($table,$column = "",$where = 1,$orderby = "",$limit = "")
{
if($column == "")
$column='*';
if($orderby == "")
$orderby = 'NULL';
if($limit != "")
{
$limit = "LIMIT ".$limit;
}
$result = $this -> query("SELECT ".$column." FROM ".$table." WHERE ".$where." ORDER BY ".$orderby."".$limit."");
if($this -> error)
{
return $this -> error;
}
else
{
$index = 0;
while($row = $result -> fetch_assoc())
{
$data[$index] = $row;
}
return $data;
}
}
public function delete($table,$where)
{
$this -> query("DELETE FROM ".$table." WHERE ".$where."");
if($this -> error)
{
return $this -> error;
}
else
{
return true;
}
}
}