0

someone can tell me where am I wrong? why i have this message "Fatal error: Call to a member function query() on a non-object" when i try call query function? thank you :)

class Connection extends mysqli {

 protected $user;
 protected $password;
 protected $database;
 protected $host;
 protected $querymia;
 protected $link;



 function __construct($user,$pass,$database,$host){
    $this->user=$user;
    $this->password=$pass;
    $this->database=$database;
    $this->host=$host;


    $this->link= mysqli_connect("$this->host","$this->user","$this->password","$this->database") or die("Error");


 }

 public function InsertQuery($string,$table){


  $this->querymia = 'INSERT INTO' . "$table" . 'VALUE' . '(' . "$string".')' ;
  $this->link->query($this->querymia); 


 }


}
Gino81
  • 11
  • 3

2 Answers2

1

The procedural mysqli_connect returns a handler, not an object.

Therefore, you should be using mysqli_query($this->link, $this->querymia);

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0

The whole error should state: Fatal error: Call to a member function query() on a non-object. So $this->link is not an object. Maybe the connection couldn't be estabilished.

You should add some code like:

if (!$this->link) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

after calling mysqli_connect() or before calling query();

Fra H
  • 345
  • 1
  • 5
  • 20
  • mmmh no the connection is ok, I want to distinguish the database connection in the costructor and query in the function, I do not understand how to do this. – Gino81 Jan 27 '14 at 16:57
  • Your code is ok. Just add this before `$this->link->query($this->querymia);` – Fra H Jan 27 '14 at 17:06
  • sorry, what should I add before? :) – Gino81 Jan 27 '14 at 17:17
  • hello,I resolved, the real problem is that i instantiated the children class but not parent class (whit connection database), thanks you to all! – Gino81 Jan 28 '14 at 08:53