我翻看了以前所有類似的問題,它們都有不同的代碼,所以找不到答案!程序返回:致命錯誤:調用非對象的成員函數fetch()
我的問題是我的程序返回
請求時從MySQL數據庫信息時,我的PHP代碼運行Fatal error: Call to a member function fetch() on a non-object
。這裏是我的代碼:
<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>";
}
}
?>
,因爲我有一個打印語句,以確保它確實連接到數據庫。 (MDP - >密碼)*
它沒有得到任何istance,你需要找出原因http://php.net/ manual/en/pdo.error-handling.php - http://php.net/manual/en/function.error-reporting.php –
'$ requete =「選擇mdp,輸入FROM AY_users WHERE login ='」。$ _POST ['login']。「'」;'這應該能夠解決您的錯誤,但仍然會讓您容易受到SQL注入攻擊。 –
另外,利用準備好的語句來防止SQL注入攻擊。 http://php.net/manual/en/pdo.prepared-statements.php –