【问题标题】:Call to undefined method PDO::execute()调用未定义的方法 PDO::execute()
【发布时间】:2014-06-15 18:31:50
【问题描述】:

我正在尝试编写页面登录代码,但我被这个错误阻止了

pliz在这里告诉我错误的事情

<?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"];
        }
    }

}
?>

【问题讨论】:

  • execute()PDOStatement 不是PDO 的方法。您在这里使用 PDO 的方式有点混乱。
  • pliz 你能给我正确的语法吗
  • 您可能正在考虑PDO::exec(),尽管这也不适合您的查询。在执行SELECT 语句时,您应该使用PDO::query()。此外,您的代码存在 SQL 注入问题。您应该花时间阅读 PDO 的预处理语句。

标签: php pdo


【解决方案1】:

-&gt;execute() 用于准备好的语句。例如

$stmt = $dbh->prepare('some query here');
$stmt->execute();

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

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

您确实应该查看准备好的语句。您很容易受到SQL injection attacks 的攻击,而且由于您一开始就使用 PDO,因此不编写安全查询基本上是不可原谅的。

【讨论】:

  • +1 个小错字,我假设您的意思是“-&gt;execute() 用于准备好的语句”?
猜你喜欢
  • 1970-01-01
  • 2015-08-30
  • 2018-04-11
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多