gpt4 book ai didi

php - 调用未定义的方法 PDO::execute()

转载 作者:行者123 更新时间:2023-11-29 20:08:14 25 4
gpt4 key购买 nike

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

请告诉我这里的错误

<?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() 用于准备好的语句。例如

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

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

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

你确实应该研究一下准备好的语句。您很容易受到 SQL injection attacks 的攻击照原样,由于您一开始就使用 PDO,所以不编写安全查询基本上是不可原谅的。

关于php - 调用未定义的方法 PDO::execute(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40268745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com