我正在创建一个 PDO 登录类以在我的项目中使用,但由于我是新手,因此无法将参数绑定到准备好的 sql 语句。这是要做到这一点的功能:
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";
}
}
}
这是连接:
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;
}
}
我收到以下错误:
致命错误:在第 22 行......中调用未定义的方法 PDO::bindParam()
如果有人可以帮助我,那就太好了,我真的很想更好地了解 PDo。