PHP中的查询不会提取系统中已有的数据

PHP中的查询不会提取系统中已有的数据,php,sql,Php,Sql,我试图检查数据库中是否存在给定的电子邮件,但返回的查询为空,我不确定原因 我尝试手动输入表中记录的电子邮件地址,但仍然不起作用 user.php: function emailExists() { $query = "SELECT id, firstname, lastname, access_level, password, status from " . $this->table_name . " where email = ?"; //prepare this

我试图检查数据库中是否存在给定的电子邮件,但返回的查询为空,我不确定原因

我尝试手动输入表中记录的电子邮件地址,但仍然不起作用

user.php:

function emailExists()
{

    $query = "SELECT id, firstname, lastname, access_level, password, 
status from " . $this->table_name . " where email = ?";

    //prepare this query
    $stmt = $this->conn->prepare($query);
login.php:

//get database connection
$database = new Database();
$db = $database->getConnection();

//initialize objects
$user = new User($db);

//check if email and password are in the database
$user->email=$_POST['email'];

//check if email exists, also get user details using this emailExists() method
$email_exists = $user->emailExists();
数据库类:

class Database{

    // specify your own database credentials
    private $host = "localhost";
    private $db_name = "games_for_sale";
    private $username = "root";
    private $password = "";
    public $conn;

    // get the database connection
    public function getConnection(){

        $this->conn = null;

        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}
用户类别:

class User{
    //database connection
    private $conn;
    private $table_name = "users";

    //object properties
    public $id;
    public $firstname;
    public $lastname;
    public $email;
    public $contact_number;
    public $address;
    public $password;
    public $access_level;
    public $access_code;
    public $status;
    public $created;
    public $modified;

    //constructor
    public function _construct($db){
        $this->conn = $db;
    }

    //check if given email exist in the database
    function emailExists()
    {

        $query = "SELECT id, firstname, lastname, access_level, password, status from " . $this->table_name . " where email = ?";

        //prepare this query
        $stmt = $this->conn->prepare($query);

        //sanitize
        $this->email = htmlspecialchars(strip_tags($this->email));

        //bind given email value
        $stmt->bindParam(1, $this->email);

        //execute the query
        $stmt->execute();

        //get number of rows
        $num = $stmt->rowCount();

        //if email exists, assign values to object properties for easy access and use for php sessions
        if ($num > 0) {

            //get record details / values
            $row = $stmt->fetch(PDO::FETCH_ASSOC);

            //assign values to object properties
            $this->id = $row['id'];
            $this->firstname = $row['firstname'];
            $this->lastname = $row['lastname'];
            $this->access_level = $row['access_level'];
            $this->password = $row['password'];
            $this->status = $row['status'];

            //return true because email exists in the database
            return true;
        }

        //retturn false if email does not exist in the database
        return false;
    }

}
我得到的错误是:

致命错误:未捕获错误:调用上的成员函数prepare() user.php:34堆栈跟踪中为空:
#0 login.php(34):用户->电子邮件存在() #在第34行的user.php中抛出1{main}


您在哪里设置
$this->conn
?显示更多类,请
$this->conn
可能未定义
$this->conn
为空,这意味着您无法创建pdo/mysqli连接。这是数据库,但
emailExists
在您的用户类中,因此我们需要看到。
\u构造($db)
应该是
\u构造($db)
在哪里设置
$this->conn
?显示更多类,请
$this->conn
可能未定义
$this->conn
为空,这意味着您无法创建pdo/mysqli连接。这是数据库,但
emailExists
在您的用户类中,因此我们需要看到。
\u-construct($db)
应该是
\u-construct($db)