Behseini
Behseini

Reputation: 6330

Getting Error on PHP OOP Database Class __construct()

I am trying to set up a basic database class using OOP in PHP and Mysqli and here is the code I have

class AppDB {
    private $host     = "localhost";
    private $user     = "root";
    private $password = "";
    private $db       = "test";
    protected $_mysql;


  public function __construct(){
       $this->_mysql = new mysqli($host, $user, $password, $db);
          if ($this->_mysql->connect_error){
             die($this->_mysql->connect_error);
          }
   }

}

but I am getting following errors

enter image description here

Can you please let me know why this is happening?

Thanks

Upvotes: 0

Views: 126

Answers (1)

Harsh Chunara
Harsh Chunara

Reputation: 585

Replace the code with below code

public function __construct(){
       $this->_mysql = new mysqli($this->host, $this->user, $this->password, $this->db);
          if ($this->_mysql->connect_error){
             die($this->_mysql->connect_error);
          }
   }

Upvotes: 1

Related Questions