[英]PDOStatement object with bindparam [PHP]
I have a method that I'm getting to see if a user is exists: 我有一种方法可以查看用户是否存在:
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);
}
I have never used PDO before and I'm slightly confused. 我以前从未使用过PDO,我有些困惑。 I am getting the error:
我收到错误消息:
Call to undefined method PDO::bindParam()
. Call to undefined method PDO::bindParam()
。
I have seen an answer saying it's because it is part of the PDOStatement class. 我看到一个答案说这是因为它是PDOStatement类的一部分。
By changing my code to this (removing $this->conn) fixes it: 通过将我的代码更改为此(删除$ this-> conn)可以解决此问题:
$this->statement->bindParam(':user_name', $user_name);
$this->statement->bindParam(':user_password', $user_password);
However, I have no idea why? 但是,我不知道为什么?
$this->conn
is by PDO object. $this->conn
是通过PDO对象。 What have I just done to make this work? 我刚刚做了些什么来完成这项工作?
just do 做就是了
$this->statement->bindParam()
to bind your parameters, then you can call your execute statement 绑定参数,然后可以调用execute语句
make the following changes to your function: 对您的功能进行以下更改:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.