Full error code:

Fatal error: Call to a member function query () on a non-object in / home / a1713593 / public_html / Projects / PHP Lovers Blog / libraries / Database.php on line 35

What I'm trying to accomplish:

//Create DB Object $db = new Database(); //Create Query $query = "SELECT * FROM posts"; //Run Query $posts = $db->select($query); 

The file Database.php:

 <?php class Database{ public $host = DB_HOST; public $username = DB_USER; public $password = DB_PASS; public $db_name = DB_NAME; public $link; public $error; /* * Class Constructor */ public function __consrtuct(){ //Call Connect Function $this->connect(); } /* * Connector */ private function connect(){ $this->link = new mysqli($this->host, $this->username, $this->password, $this->db_name); if(!$this->link){ $this->error = "Connection Failed: ".$this->link->connect_error; return false; } } /* * Select */ public function select($query){ $result = $this->link->query($query) or die($this->link->error.__LINE__); if($result->num_rows > 0){ return $result; } else{ return false; } } /* * Insert */ public function insert($query){ $insert_row = $this->link->query($query) or die($this->link->error.__LINE__); //Validate Insert if($insert_row){ header("Location: index.php?msg=".urlencode('Record Added')); exit(); } else{ die('Error : ('. $this->link->errno . ') '. $this -> link -> error); } } /* * Update */ public function update($query){ $update_row = $this->link->query($query) or die($this->link->error.__LINE__); //Validate Insert if($update_row){ header("Location: index.php?msg=".urlencode('Record Updated')); exit(); } else{ die('Error : ('. $this->link->errno . ') '. $this -> link -> error); } } /* * Delete */ public function delete($query){ $delete_row = $this->link->query($query) or die($this->link->error.__LINE__); //Validate Insert if($delete_row){ header("Location: index.php?msg=".urlencode('Record Deleted')); exit(); } else{ die('Error : ('. $this->link->errno . ') '. $this -> link -> error); } } }; 

Swears directly on the function SELECT . What is the error I do not understand. In the tutorial on which I do it all as one to one as mine, but it works.

  • The connect () method is done by Singleton. - Winston
  • It's great that the author himself found the error and wrote about it. Only at this level questions should not be thrown out at the forum, I think. Do not treat the community as a "debugger of someone else's code." - artoodetoo
  • Well, before I wrote here for a very long time, I myself tried to understand what the problem was, but I could not, the error message was very vague and I could not understand where the problem lies. I didn’t ask anyone, so I wrote here, what else could I do? - Tachi

2 answers 2

In general, a banal mistake, or rather a typo.

Instead:

 public function __construct() 

wrote

 public function __consrtuct() 

    The constructor is not called (due to an error in the name of the method __cons rt uct), and there is a connection to the database. The output, $this->link not initialized.

    ZY It is better to transfer $host , $port , $username and $password to the constructor, and make variables private.