简体   繁体   English

带有bindparam的PDOStatement对象[PHP]

[英]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.

相关问题 Postgres / PHP PDO :: PDOStatement-> bindParam()转换为character(1)字段 - Postgres/PHP PDO::PDOStatement->bindParam() to character(1) field PHP-PDO准备的语句,“警告:PDOStatement :: bindParam()至少需要2个参数” - PHP - PDO Prepared statment, “Warning: PDOStatement::bindParam() expects at least 2 parameters” 在PHP中,PDO :: PDOStatement-> bindParam()是否按预期工作? - In PHP does the PDO::PDOStatement->bindParam() work as it is expected? 在PDOStatement :: bindParam中绑定重载属性时的PHP注意事项 - PHP notice when binding overloaded property in PDOStatement::bindParam 如何避免PDOStatement :: bindParam与参考值混淆 - How to avoid PDOStatement::bindParam messing with the referenced value PDOStatement :: bindParam data_type参数列表 - List of PDOStatement::bindParam data_type parameters php函数错误(类PDOStatement的对象) - Php function error (Object of class PDOStatement) 与 PDOStatement::bindParam() 相比,使用 PHP mysqli_stmt::bind_param() 时 MYSQL 查询性能非常差 - MYSQL query performance very poor when using PHP mysqli_stmt::bind_param() compared to PDOStatement::bindParam() PDOStatement :: bindParam在第一次调用后不起作用 - PDOStatement::bindParam doesn't work after the first call PDOStatement :: bindParam()是否未从MySQL插入设置AI值? - PDOStatement::bindParam() not setting AI value from MySQL insert?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM