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.