所以我正在使用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登录数据是正确的,我已经确定了。
我必须在这里遗漏一些非常明显的东西,但我看不到它。任何帮助是极大的赞赏。谢谢!
答案 0 :(得分: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']);
答案 1 :(得分:0)
我真的很愚蠢,当我把它发布在这里时我压缩了我的代码并且我遗漏了这部分:
$this->mysql = $mysql;
$this->mysql = $salt;
现在杀了我。