为什么出现致命错误:在非对象上调用成员函数prepare()? [英] Why i've Fatal error: Call to a member function prepare() on a non-object?

查看:115
本文介绍了为什么出现致命错误:在非对象上调用成员函数prepare()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个PDO错误问题(

因此,对于此行$stmt = $bdd->prepare('SELECT COUNT(*) FROM utilisateurs WHERE identifiant = ?');,我有此错误:

致命错误:在...中的非对象上调用成员函数prepare()

可以帮我吗?

解决方案

很明显,您的$bdd不包含SQL连接.您必须检查SQL连接的有效性.

$bdd必须是一个对象,因此它包含一个名为prepare()的方法.

如果未正确建立连接,则变量$bdd将不是对象,因此将没有方法prepare.

现在,当您使用$bdd->prepare()时,它会引发错误,指出$bdd不是对象,因此无法使用称为prepare()

的方法.

现在,您应该在$bdd->prepare()行之前显示var_dump($bdd)以便进行检查.

I've ask a question for PDO error (here But I've a another problem with object...

Here is my code :

include('../../config/connexion-bdd.php');

$nom = $_POST['nom'];
$regexNom = '/^[a-zA-Z -]+$/';
$prenom = $_POST['prenom'];
$regexPrenom = '/^[a-zA-Z -]+$/';
$email = $_POST['email'];
$regexEmail = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
$identifiant = $_POST['identifiant'];
$regexIdentifiant = '/^[a-z1-9]+$/';
$motDePasse = $_POST['motDePasse'];
$confirmeMotDePasse = $_POST['ConfirmeMotDePasse'];
$regexMdp = '/^[a-zA-Z1-9]+$/';
$utilisateurConnecte = $_POST['utilisateur-connecte'];

if (preg_match($regexNom, $nom) && 
    preg_match($regexPrenom, $prenom) &&
    preg_match($regexEmail, $email) && 
    preg_match($regexIdentifiant, $identifiant) && 
    preg_match($regexMdp, $motDePasse) && 
    preg_match($regexMdp, $confirmeMotDePasse) &&
    $motDePasse == $confirmeMotDePasse)
    {
        $stmt = $bdd->prepare('SELECT COUNT(*) FROM utilisateurs WHERE identifiant = ?');
        $stmt->execute(array($_POST['identifiant']));
        if ($stmt->fetchColumn() == 0){
               $updt=$connect->prepare("INSERT INTO utilisateurs('nom','prenom','email','identifiant','mot_de_passe') VALUES (:nom, :prenom, :email, :identifiant, MD5(:mdp))");
               $updt->execute(array('nom'=>$nom,'prenom'=>$prenom,'email'=>$email,'identifiant'=>$identifiant,'mdp'=>$motDePasse));

               header('./gestion-utilisateur.php');
          } else {
              echo 'error 1';
          }
      } else {
          echo 'error 2';
      }

Therefore I have this error for this line $stmt = $bdd->prepare('SELECT COUNT(*) FROM utilisateurs WHERE identifiant = ?');:

Fatal error: Call to a member function prepare() on a non-object in ...

Can you help my, please ?

解决方案

It is all clear, Your $bdd is not containing a SQL connection. You have to check for the validity of the SQL connection.

$bdd must be an object, and thus it contains a method called prepare().

When the connection is not established correctly, the variable $bdd will not be an object and thus will not have a method prepare.

Now, when you use $bdd->prepare() it throws an error that $bdd is not an object, thus it could not have a method called prepare()

Now, you should present a var_dump($bdd) before the $bdd->prepare() line for use to inspect it.

这篇关于为什么出现致命错误:在非对象上调用成员函数prepare()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆