Php 程序返回:致命错误:对非对象调用成员函数fetch()
我看了所有以前问过的类似问题,它们都有不同的代码,所以找不到答案 我的问题是我的程序返回 致命错误:对非对象调用成员函数fetch() 当我的php代码在从mysql数据库请求信息时运行。这是我的密码:Php 程序返回:致命错误:对非对象调用成员函数fetch(),php,mysql,Php,Mysql,我看了所有以前问过的类似问题,它们都有不同的代码,所以找不到答案 我的问题是我的程序返回 致命错误:对非对象调用成员函数fetch() 当我的php代码在从mysql数据库请求信息时运行。这是我的密码: <body> <?php // Connexion à la base de données include_once("connexionMysql.php"); // test si bouton ok if (isset($_POST['valid'])) {
<body>
<?php
// Connexion à la base de données
include_once("connexionMysql.php");
// test si bouton ok
if (isset($_POST['valid'])) {
// construction de la req.
$requete="SELECT mdp, type FROM AY_users
WHERE login='.$_POST['login'].'";
// exécution de la requête
$reponse = $bdd->query($requete);
if ($donnees=$reponse->fetch(PDO::FETCH_ASSOC)) {
if ($donnees['mdp']==$_POST['mdp']) {
// le mot de passe est le bon
print "<br/>Authentification réussie ! <br/>\n";
if($donnees['type']==0){
header("Location: pageDaccueilAdmin.php");
}
if($donnees['type']==1){
header("Location: pageDaccueilEnseignant.php");
}
if($donnees['type']==2){
header("Location: pageDaccueilCher.php");
}
} else {
print "Le mot de passe n'est pas le bon, veuillez reessayer<br/>";
print "<a href='authentif.php'>Se reconnecter</a>";
}
} else {
// sinon : login inexistant
print "Ce login est inexistant, veuillez <br/>\n";
print "1- <a href='authentif.php'>Se reconnecter</a>";
print "2- <a href='creer.php'>créer un compte</a>";
}
}
?>
PDO::query()
在失败时返回PDOStatement对象,或FALSE
您的查询失败(返回false)。这就是为什么会出现非对象错误:$response
不是对象。在这种情况下是bool(false)
。您的查询没有从数据库中获得任何距离,您的引号格式错误
这样试试
$requete="SELECT mdp, type FROM AY_users
WHERE login='" .$_POST['login']. "'";
// exécution de la requête
$reponse = $bdd->query($requete);
它失败了,您需要找出原因-$requete=“选择mdp,从AY_用户中键入,其中login='”$\u POST['login']。”
可以修复错误,但仍会使您容易受到SQL注入攻击。此外,请使用准备好的语句来防止SQL注入攻击。啊,好吧,我想这就行了!谢谢,如果工作,请将答案标记为已接受