我有一种方法可以查看用户是否存在:
public function login_user($user_name, $user_password){
$this->statement = $this->conn->prepare('SELECT * FROM users WHERE user_name=:user_name AND user_password=:user_password');
$this->statement = $this->conn->bindParam(':user_name', $user_name);
$this->statement = $this->conn->bindParam(':user_password', $user_password);
$this->statement->execute();
return $this->statement->fetch(PDO::FETCH_ASSOC);
}
我以前从未使用过PDO,我有些困惑。我收到错误消息:
Call to undefined method PDO::bindParam()
。
我看到一个答案说这是因为它是PDOStatement类的一部分。
通过将我的代码更改为此(删除$ this-> conn)可以解决此问题:
$this->statement->bindParam(':user_name', $user_name);
$this->statement->bindParam(':user_password', $user_password);
但是,我不知道为什么?$this->conn
由PDO对象。我刚刚做了些什么来完成这项工作?
对您的功能进行以下更改:
public function login_user($user_name, $user_password){
//prepare the query
$query='SELECT * FROM users WHERE user_name=:user_name AND user_password=:user_password';
$statement = $this->conn->prepare($query);
//bind the parameters
$statement->bindParam(':user_name', $user_name);
$statement->bindParam(':user_password', $user_password);
//excute & fetch the data
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句