PHP:在非对象上调用成员函数prepare()

时间:2013-03-09 13:24:57

标签: php mysqli

我的PHP功能非常简单。它只是使用预准备语句来查询数据库,但我无法理解为什么它不起作用。

即使我已将$conn声明为私有变量并使用$this->conn

,我仍然会收到'对非对象'错误调用成员函数prepare()
<?php

require_once 'constants.php';

class Mysql {

    private $conn;

    function ___construct(){

        $this->conn = new mysqli (DB_SEVER, DB_USER, DB_PASSWORD, DB_NAME);
        if (!$this->conn){
            die('There was a problem connecting to the database!');
        }

    }

    function verify_name_and_pass ($un, $pwd) {

        $query = "SELECT * FROM users 
                  WHERE username = ? AND user_password = ?
                  LIMIT 1";

        if ($stmt = $this->conn->prepare($query)) {

            $stmt->bind_param('ss', $un, $pwd);
            $stmt->execute();

            if ($stmt->fetch()) {
                $stmt->close();
                return true;            
            }

        }       
    }   
}

1 个答案:

答案 0 :(得分:2)

问题在于您的__construct()功能。至少有一个拼写错误:

  • __construct()拼写为2 _,拼写为3。
  • 您可能打算使用DB_SERVER代替DB_SEVER

此外,一旦确定$this->conn不是null,您应该通过阅读$this->conn->connect_errno并确保0确认连接是否已正确建立。

相关问题