0

所以我在 php 中处理一个简单的用户类,它有一个包含 mysqli 对象的类变量,但是我不断收到错误:

致命错误:在第 X 行* /classes/user.php中的非对象上调用成员函数 real_escape_string()

我已经检查了所有内容,它应该可以工作,但它没有。不知何故。这是我的代码:

namespace bibliotheek;

class user
{
    private $mysql;

    private $logged_in = false;
    private $user_data = null;      //ARRAY: user_id, e-mail, password, bevoegdheid, naam, achternaam, adres, postcode, stad

    function __construct(\mysqli $mysql, $salt)
    {
        $this->mysql = $mysql;
    }

    public function login($email, $pass, $hash = false)
    {
        $email = $this->mysql->real_escape_string($email);
        if($hash == false)
            $pass  = sha1($this->salt.$pass);

        $query  = "SELECT *
                   FROM gebruikers
                   WHERE gebruikers.email = '$email' AND gebruikers.password = '$pass'";

        $result    = $this->mysql->query($query);
        $user_data = $result->fetch_assoc();

        if($user_data == null)
            return;

        $this->logged_in = true;
        $this->user_data = $user_data;
        $this->create_cookies($email, $pass);
    }
}

这就是 mysqli 对象传递给类的方式:

$mysql  = new mysqli($cfg['mysql_server'], $cfg['username'], $cfg['password'], $cfg['database']);
$user   = new bibliotheek\user($mysql, $cfg['salt']);

mysql登录数据是正确的,我已经确定了。

我必须在这里遗漏一些非常明显的东西,但我就是看不到它。任何帮助是极大的赞赏。谢谢!

4

2 回答 2

0

这就是它应该的样子

error_reporting(E_ALL);

$mysql  = new mysqli($cfg['mysql_server'], $cfg['username'], $cfg['password'], $cfg['database']);
if ( !$mysql )
{
    throw new Exception(mysqli_connect_error()));
}
$user = new bibliotheek\user($mysql, $cfg['salt']);
于 2013-04-07T20:21:13.947 回答
0

我真的很愚蠢,当我在这里发布代码时,我稍微压缩了我的代码,我省略了这部分:

    $this->mysql = $mysql;
    $this->mysql = $salt;

现在杀了我。

于 2013-04-07T21:05:39.623 回答