2016-04-12 33 views
0

我翻看了以前所有類似的問題,它們都有不同的代碼,所以找不到答案!程序返回:致命錯誤:調用非對象的成員函數fetch()

我的問題是我的程序返回

Fatal error: Call to a member function fetch() on a non-object

請求時從MySQL數據庫信息時,我的PHP代碼運行

。這裏是我的代碼:

<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 - >密碼)*

+0

它沒有得到任何istance,你需要找出原因http://php.net/ manual/en/pdo.error-handling.php - http://php.net/manual/en/function.error-reporting.php –

+3

'$ requete =「選擇mdp,輸入FROM AY_users WHERE login ='」。$ _POST ['login']。「'」;'這應該能夠解決您的錯誤,但仍然會讓您容易受到SQL注入攻擊。 –

+0

另外,利用準備好的語句來防止SQL注入攻擊。 http://php.net/manual/en/pdo.prepared-statements.php –

回答

0

宥都錯了格式化引號查詢不從數據庫

嘗試這種方式

$requete="SELECT mdp, type FROM AY_users 
     WHERE login='" .$_POST['login']. "'"; 
// exécution de la requête 
$reponse = $bdd->query($requete); 
+0

啊好吧,認爲它得到它的工作!謝謝 –

+0

如果工作,請將答案標記爲已接受 – scaisEdge

0

PDO::query()返回PDOStatement對象物體,或者在失敗FALSE

您的查詢失敗(返回false)。這就是爲什麼你得到非對象錯誤:$response不是一個對象。在這種情況下,它是bool(false)

相關問題