嗯,我試圖擴展名爲數據庫的繼承,它可以輕鬆地檢索數據庫信息(如果正確地完成一行)的類mysqli。它不工作了,到目前爲止,因爲我收到以下錯誤:mysqli及其繼承錯誤
這裏所定義的數據庫類:
class Database extends mysqli{
private $select, $create, $insert, $alter, $update, $delete, $drop;
protected $mysql, $result, $table, $column, $where, $value, $limit, $order, $type;
public function __construct($host, $user, $pass, $db){
$this->mysql = new mysqli($host, $user, $pass, $db)
or die("Error connecting to the database {$db}");
}
public function __destruct(){
$this->mysql->close();
}
protected function prepareQuery(){
if ($prepare = $this->mysqli->prepare($this->query)) {
trigger_error("Problem preparing query ($this->query) ".$this->mysqli->error, E_USER_ERROR);
}
return $prepare;
}
protected function reset(){
unset($this->table);
unset($this->column);
unset($this->where);
unset($this->value);
unset($this->limit);
unset($this->order);
}
public function select($column){
$this->select = implode(",", $column);
return $this;
}
public function table($table){
$this->table = $table;
return $this;
}
public function where($where, $comparison = "=", $logic){
$i = 0;
foreach ($where as $col => $val){
$wherestring .= (is_array($comparison)) ? " {$col} {$comparison[$i]} '{$val}'" : " WHERE {$col} {$comparison} '{$val}'";
$wherestring .= ($i < (count($where)-1))?" {$logic[$i]}" :" ";
$i++;
}
$this->where = $wherestring;
return $this;
}
public function limit($limit){
$this->limit = $limit;
return $this;
}
public function order($order){
$this->order = $order;
return $this;
}
public function runquery($method){
$query = "{$method} {$this->select} FROM {$this->table}";
if(!empty($this->where)) $query .= " WHERE {$this->where}";
if(!empty($this->limit)) $query .= " LIMIT {$this->limit}";
if(!empty($this->order)) $query .= " ORDER BY {$this->order}";
echo "The generated Query is: \n".$query;
$this->result = parent::query($query);
$result = parent::fetch_array($this->result);
return $result;
}
}
這是我從一個腳本文件,運行方式:
include("inc/config.php");
include("classes/class_data.php");
$db = new Database($dbhost, $dbuser, $dbpass, $dbname);
$row = $db->select(array("password","email"))->table($prefix."users")->where(array("uid"=>1, "username"=>Admin"),array("=","="),array("AND"))->limit(2)->order("uid")->runquery("SELECT");
它沒有工作,我得到了下面的警告和錯誤消息: 代碼:
Warning: mysqli::query() [mysqli.query]: Couldn't fetch Database in classes/class_data.php on line 70
Fatal error: Call to undefined method mysqli::fetch_array() inclass_data.php on line 71
我現在有點困惑,因爲我似乎找不到解決這個問題的方法。你能幫助任何人嗎?我非常感激。
謝謝
哦,我的答案已經被取代,xdazz現在也在說這個... –