Not claiming this is necessarily how I'd do it, but it should give you the idea:
<?php
class MyDB extends mysqli
{
private $table;
private $columns = array();
private $where = array();
public function __construct($host, $user, $pwd, $database)
{
parent::__construct($host, $user, $pwd, $database);
}
public function select($column)
{
$columns = preg_split('/\s*,\s*/', trim($column));
foreach($columns as $col) {
$this->columns[] = "`$col`";
}
return $this; // <----------------
}
public function from($table)
{
$this->table = $table;
return $this; // <---------------
}
public function where($col, $val)
{
$str = "`$col` = ";
if(is_numeric($val)) {
$str .= $val;
} else {
$str .= "'" . $this->real_escape_string($val) . "'";
}
$this->where[] = $str;
return $this; // <---------------
}
public function exec()
{
$sql = "SELECT " . implode(',', $this->columns) . " FROM `" .
$this->table . "`";
if(!empty($this->where)) {
$sql .= " WHERE " . implode (" AND ", $this->where);
}
// DEMO ONLY:
echo $sql;
// actual code would execute query and return MySQLi_Result object
}
}
// DEMO:
$test = new MyDB('localhost', '*****', '*****', 'db_name');
$test->select('foo,bar')
->from('fubar')
->where('id', 1)
->where('name', 'John')
->exec();
Output:
SELECT `foo`,`bar` FROM fubar WHERE `id` = 1 AND `name` = 'John'