Mysqli:致命错误:在null上调用成员函数prepare()

时间:2015-09-26 18:31:32

标签: php authentication mysqli

我试图将登录函数转换为具有某些函数的类,它一直在工作,直到我将其转换为类,这是我的代码:

class merwaa_login {
public $conn;
public function __construct($conn) {
    $this->conn = $conn;
    if(isset($_POST['login-submit'])) {
    $username = merwaa_clean($_POST['username']);
    $password = merwaa_clean($_POST['password']);
    $sql = $conn->prepare("SELECT * FROM users WHERE username = ? OR email = ?;");
    $sql->bind_param('ss', $username, $username);
    $sql->execute();
    $all = $sql->get_result();
    $all = $all->fetch_assoc();
    if (count($all) === 0){
        echo 'البيانات المدخلة خاطئة!';
    }
    else{
        $hashed_pwd = $all['password'];
        if (password_verify($password,$hashed_pwd)) {
            echo 'كفو';
            $_SESSION['username'] = $username = $all['username'];
            if(isset($POST['rememberme'])) {
                $uid = $all['ID'];
                merwaa_set_jwt($username,$uid);
            }

        } else{
            echo 'خطأ';
        }
    }
}
}
}

我可能会遇到一些语法错误,请注意我。

2 个答案:

答案 0 :(得分:0)

更改: -

$sql = $conn->prepare("SELECT * FROM users WHERE username = ? OR email = ?;");

对此: -

$sql = $this->conn->prepare("SELECT * FROM users WHERE username = ? OR email = ?;");

或者,您可以将public $conn更改为global $conn。然后你不需要做出上述改变。

答案 1 :(得分:0)

尝试使用y代替$ conn-> prepare 并且应该将正确的连接对象传递给构造函数