Call to a member function prepare() on a non-object in (Fatal error)

后端 未结 2 1911
青春惊慌失措
青春惊慌失措 2021-01-29 10:34

Fatal error: Call to a member function prepare() on a non-object in G:\\xampp\\htdocs\\live\\Billing Suryas\\model\\DBConfig.php on line 28

<
2条回答
  •  梦谈多话
    2021-01-29 11:03

    You write a class with these properties:

    class Database
    {   
        private $host = "localhost";
        private $db_name = "new_suryas1";
        private $username = "root";
        private $password = "";
        public $conn;
    

    Inside a class method, variable scope is same as functions: external variables are not accessible.

    To access to class properties inside a class method you have to use $this:

    $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
    $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    
    (...)
    
    $stmt = $this->conn->prepare("select * from users where user_name=:uname and password=:paswrd and permission='0' and delet='0'");
    

    • Read more about variable scope
    • Read more about What does the variable $this mean in PHP?

提交回复
热议问题