所以我在 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登录数据是正确的,我已经确定了。
我必须在这里遗漏一些非常明显的东西,但我就是看不到它。任何帮助是极大的赞赏。谢谢!