对中的非对象调用成员函数prepare()(致命错误)


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

致命错误:在第28行的G:''examplep''htdocs''live''Billing Suryas''model''DBConfig.php中的非对象上调用成员函数prepare()

<?php
    class Database
    {   
        private $host = "localhost";
        private $db_name = "new_suryas1";
        private $username = "root";
        private $password = "";
        public $conn;
        public function dbConnection()
        {
            $this->conn = NULL;    
            try
            {
                $conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
            }
            catch(PDOException $exception)
            {
                echo "Connection error: " . $exception->getMessage();
            }
            return $conn;
        }
        public function login($usname,$uspswd)
        {
            try
            {
                $stmt =$conn->prepare("select * from users where user_name=:uname and password=:paswrd and permission='0' and delet='0'");
                $stmt->execute(array(':uname'=>$usname, ':paswrd'=>$uspswd));
                $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
                if($stmt->rowCount() == 1)
                {
                    if(password_verify($uspswd, $userRow['password']))
                    {
                        $_SESSION['user_session'] = $userRow['user_id'];
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch(PDOException $exception)
            {
                echo $exception->getMessage();
            }
        }
    }
    ?>

这是我的DBConfig.php

我找不到我的代码中有什么错误

有人帮我吗。。。

您编写了一个具有以下属性的类:

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

在类方法内部,变量作用域和函数相同:外部变量是不可访问的。

要访问类方法中的类属性,必须使用$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'");

  • 阅读有关变量作用域的更多信息
  • 阅读更多关于变量$this在PHP中的含义

您正试图将DB连接句柄存储在变量$conn中。但这只是一个局部变量,而不是类属性$conn。要在以后使用,您必须访问它是$this->conn

所以你的连接方法应该类似

public function dbConnection()
{
    try {
        $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); 
    } catch(PDOException $exception) {
        echo "Connection error: " . $exception->getMessage();
    }
}

假设您现在可以在其他功能中使用$this->conn

public function login($usname, $uspswd)
{
    try {
        $stmt = $this->conn->prepare("select * from users where user_name=:uname and password=:paswrd and permission='0' and delet='0'");
        $stmt->execute(array(':uname'=>$usname, ':paswrd'=>$uspswd));
        $userRow = $stmt->fetch(PDO::FETCH_ASSOC);
        if($stmt->rowCount() == 1) {
            if(password_verify($uspswd, $userRow['password'])) {
                $_SESSION['user_session'] = $userRow['user_id'];
                return true;
            } else {
                return false;
            }
        }
    } catch(PDOException $exception) {
        echo $exception->getMessage();
    }
}

换句话说:使用$conn而不是$this->conn不是引用对象属性$conn,而是方法中的局部变量$conn。但是局部变量是而不是持久的。因此,一旦您离开方法dbConnection(),并尝试通过在login()中选择相同的名称来重用该变量,标识符$conn实际上指的是两个不同的局部变量。这当然意味着它在login()方法中不包含任何值,这会导致您收到的错误。

还有其他方法可以做到这一点,比如在方法中"注入连接对象"。但以上是一个干净的,通常是首选的方法。