Jump to content

errors with mysqli and its inheritance...


Hall of Famer

Recommended Posts

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:

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.

Link to comment
Share on other sites

A few things caught my attention...

here

  public function __construct($host, $user, $pass, $db){
    $this->mysql = new mysqli($host, $user, $pass, $db)   // it not should be $this->mysqli to be consistent with the usage after this?
	   or die("Error connecting to the database {$db}");

 

and this

    $this->result = parent::query($query);
    $result = parent::fetch_array($this->result);

should be

    $this->result = $this->mysqli->query($query);
     $result = $this->result->fetch_array();

 

 

and finally...

- you are missing a " before Admin" and

- LIMIT should be the last part of your select... hence your select could be invalid

 

with those modifications your code works for me

 

Link to comment
Share on other sites

Well to make things clear, I have the select() method that looks like this right now:

 

  public function select($action = ""){
    if(empty($this->column)) $this->column = "*";
    $query = "SELECT {$this->column} FROM {$this->table}";
$query .= (!empty($this->where))?" WHERE {$this->where}":"";
$query .= (!empty($this->order))?" ORDER BY {$this->order}":"";
$query .= (!empty($this->limit))?" LIMIT {$this->limit}":"";
    echo $query."<br>";
    $this->result = $this->mysqli->query($query);
    if($action == "row") $result = $this->result->fetch_row(); 
    if($action == "assoc") $result = $this->result->fetch_assoc();
    if($action == "array") $result = $this->result->fetch_array();
    if($action == "object")  $result = $this->result->fetch_object();
    if($action == "num") $result = $this->result->num_rows();
    $this->reset();
    return $result;
  }

 

Theoretically it should check the variable $action, which can be of any values that retrieves mysql data such as row, array, assoc, object. I just looked at mysqli's manual and found that there are still many more possible ways to fetch data. I want to use a protected method called fetch(), which also accepts the variable $action and returns results from such as mysqli->fetch_array() and mysqli->fetch_object(). If works, the script should look like this one below:

 

  public function select($action = ""){
    if(empty($this->column)) $this->column = "*";
    $query = "SELECT {$this->column} FROM {$this->table}";
$query .= (!empty($this->where))?" WHERE {$this->where}":"";
$query .= (!empty($this->order))?" ORDER BY {$this->order}":"";
$query .= (!empty($this->limit))?" LIMIT {$this->limit}":"";
    echo $query."<br>";
    $this->result = $this->mysqli->query($query);
    $result = $this->result->fetch($action)
    $this->reset();
    return $result;
  }

protected function fetch($action){
   switch($action){
     case "row":
     // fetch data using mysqli->fetch_row()
     case "assoc":
     // fetch data using mysqli->fetch_assoc()
     case "array":
     // fetch data using mysqli->fetch_array()
     case "object":
     // fetch data using mysqli->fetch_object()
     case "num":
     // returns the number of rows...
     ...
     default:
     // returns nothing
   }
  
}

 

How can I design such a protected method so that it can be accessed within the select() method and other possible future methods I write(such as mselect(), which is used to select data from more than one table)? Please help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.