开发者

errors with mysqli and its inheritance

Well I tried to extend the class mysqli with its inheritance called Database, which can retrieve database info easily(in one line if done correctly). It does not work out so far, as I am getting the following errors:

Here is the Database Class as defined:

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;
  } 

}

And this is the way I run it from a script file:

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");

It did not work, and I got the following warning and error messages: Code:

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

I am a bit confused now since I cant seem to figure out a way to resolve this problem. Can anyone of you please help? Id appreciate it very much.

Thank you


fecth_array() is not the method of mysqli but the MySQLi_STMT class.

And your should not design like that, you are doing thing that extend mysqli, but you also do $this->mysql = new mysqli(...), between is-A and has-A, you should just choose one. I recommend has-A.


In addition to what xdazz said, you are also mixing the “inherits from” and the “uses” patterns: you have a mysqli instance named $this->mysql as well as $this, inherited from the parent:: mysqli. You should decide on either one but not use both. If you want the user of your class to be able to to do anything with it that she can to with a normal mysqli instance (including calling all the superclass methods herself), use inheritance. If you want to restrict the use cases, use a mysqli type instance variable and remove the inheritance. You can then selectively proxy methods of the inner $this->mysql to allow users to call them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜