Php mysqli及其继承的错误
我尝试用名为Database的继承来扩展mysqli类,如果操作正确,它可以在一行中轻松检索数据库信息。到目前为止还不可行,因为我得到了以下错误: 以下是定义的数据库类:Php mysqli及其继承的错误,php,mysql,inheritance,mysqli,mysql-management,Php,Mysql,Inheritance,Mysqli,Mysql Management,我尝试用名为Database的继承来扩展mysqli类,如果操作正确,它可以在一行中轻松检索数据库信息。到目前为止还不可行,因为我得到了以下错误: 以下是定义的数据库类: class Database extends mysqli{ private $select, $create, $insert, $alter, $update, $delete, $drop; protected $mysql, $result, $table, $column, $where, $value, $
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;
}
}
它不起作用,我收到以下警告和错误消息:
代码:
我现在有点困惑,因为我似乎无法找到解决这个问题的方法。你们谁能帮忙吗?我非常感激
谢谢fecth_数组不是mysqli的方法,而是mysqli_STMT类
你们不应该这样设计,你们在做扩展mysqli的事情,但你们也在做$this->mysql=newmysqli…,在is-A和has-A之间,你们应该选择一个。我推荐has-A.除了xdazz所说的,您还混合了“继承自”和“使用”模式:您有一个名为$this->mysql的mysqli实例,以及$this,继承自父::mysqli。你应该选择其中一个,但不能同时使用两个。如果您希望您的类的用户能够用它做任何她可以用普通mysqli实例做的事情,包括自己调用所有超类方法,请使用继承。如果要限制用例,请使用mysqli类型实例变量并删除继承。然后,您可以有选择地代理内部$this->mysql的方法,以允许用户调用它们。哦,我的答案刚刚被取代,xdazz现在也这么说…
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