PDO::execute() 方法未定义

5
我正在尝试编写一个登录页面,但在此错误处停顿:
请告诉我这里有什么问题。
<?php
@session_start();
include("../../connexion/connexion.php");
class login_class {
        public $user;
        public $password;
        public $connexion;
    public function check_login() {
        try {
            $cn = new class_connect();
            $this->connexion = $cn->connect(null);
            $result = $this->connexion->execute("select * from user where username='$this->user' and password='$this->password'");

                        $data = $result->fetchAll(PDO::FETCH_OBJ);


            if (!empty($data[0]->id_user)) {
                return true;
            }else {
                return false;
            }
        }catch(PDOException $ex) {  
            echo $ex->getMessage();
        }
    }
    public function __construct($user) {
        if($user){
            $this->user = $user["username"];
            $this->password = $user["password"];
        }
    }

}
?>

1
execute()PDOStatement 的一个方法,而不是 PDO 的。你在这里使用 PDO 的方式有点混淆了。 - Darragh Enright
请问能否给我正确的语法格式? - user3531649
你可能正在考虑使用 PDO::exec(),但这也不适用于你的查询。在执行 SELECT 语句时,应该使用 PDO::query()。此外,你的代码存在 SQL 注入问题。你应该花时间了解一下 PDO 的预处理语句。 - Darragh Enright
1个回答

15
< p > ->execute() 用于预处理语句。例如:

->execute() 用于执行预处理语句。例如:
$stmt = $dbh->prepare('some query here');
$stmt->execute();

您正在尝试直接在主数据库对象上执行查询。对于PDO来说,这意味着

 $dbh->exec('query goes here');

你真的应该去研究一下预处理语句。你目前容易受到SQL注入攻击,而且既然你已经在使用PDO了,不写安全查询基本上是不可原谅的。


+1 有一个小错别字,我猜你的意思是"->execute()是用于预处理语句"? - Darragh Enright

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接