[英]Cant execute my PDO query using “execute()” getting “bindParam()” errors
I'm creating a PDO Login class to use on my projects, but since I'm new to it I'm not being able to bind parameters to a prepared sql statement. 我正在创建要在项目中使用的PDO登录类,但是由于我是新手,因此无法将参数绑定到准备好的sql语句。 Here's the function that is ment to do it : 这是执行此操作的功能:
include_once('connection.php');
class User{
protected $db;
public function __construct(){
$oConnection = new Connection;
$this->db = $oConnection->getConnection();
//var_dump($this->db);
}
public function Login($name, $pass){
if(!empty($name) && !empty($pass)){
$st = $this->db;
$st->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();
var_dump($st);
if($st->rowCount == 1){
echo "User verified, Acces granted.";
}else{
echo "Incorrect username or password.";
}
}else{
echo "Please fill in the entire form";
}
}
}
And here is the Connection : 这是连接:
class Connection{
protected $db;
//Construct
public function Connection(){
$conn = NULL;
try{
$conn = new PDO("mysql:host=localhost;dbname=<db_name>", "<db_user>", "<db_pass>");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
echo 'ERROR: ' . $e->getMessage();
}
$this->db = $conn;
}
public function getConnection(){
return $this->db;
}
}
I am Receiving the following error : 我收到以下错误:
Fatal error: Call to undefined method PDO::bindParam() in ......... on line 22 致命错误:在第22行的.........中调用未定义的方法PDO :: bindParam()
If someone can help me out a bit that would be great, I really want to get to know PDo better. 如果有人可以帮我一点忙,那我真的很想更好地了解PDo。
You have to capture the result of the prepare call (which is a PDOStatement object) and then call bindParam()
on that, not the PDO object itself. 您必须捕获prepare调用(这是一个PDOStatement对象)的结果,然后在该对象上而不是PDO对象本身上调用bindParam()
。
$st = $this->db->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();
$st
is now the PDOStatement object and you can call bindParam()
and execute()
. $st
现在是PDOStatement对象,您可以调用bindParam()
和execute()
。
If you have copied your MySQL error to google you would see same errors in many many pages. 如果您已将MySQL错误复制到Google,则会在许多页面中看到相同的错误。 Here is the first one saying: 这是第一句话:
The bindParam()
method is inside the PDOStatement
class, not the PDO class. bindParam()
方法位于PDOStatement
类而不是PDO类中。 The statement is the result of the prepare() method. 该语句是prepare()方法的结果。
Please see: Call to undefined method PDO::bindParam() 请参阅: 调用未定义的方法PDO :: bindParam()
You are using PDO object as statement prepared object to bind 您正在使用PDO对象作为语句准备对象进行绑定
$db = $this->db;
$st = $db->prepare("SELECT * FROM users WHERE user_name=? and user_password=?");
$st->bindParam(1, $name);
$st->bindParam(2, $pass);
$st->execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.