Help me = Error: Call to a Member Function prepare() on null in D: wamp www New folder user classes.php on line 29

Asked

Viewed 55 times

-1

They taught me this: But it doesn’t work for me says Error: Call to a Member Function prepare() on null in D: wamp www New folder user classes.php on line 29.

<?php  

Class usuarios
{
    private $pdo;
    public $msgErro = "";

    public function conectar($nome, $host, $email, $senha)
    {

      global $pdo;
      try
      {
          $pdo = new PDO("mysql:dbname=".$nome.";host".$host,);
       } catch (PDOException $e) {
          $msgErro = $e->getMessage(); 

       }


    }



   public function cadastrar($nome, $telefone, $email, $senha)
   {

       global $pdo;
       $sql = $pdo->prepare("SELECT id_usuarios FROM usuarios WHERE email = :e");
       $sql->bindValue(":e",$email);
       $sql->execute();
       if($sql->rowCount() > 0)
       {  
          return false;
       }
       else
       {
        $sql = $pdo->prepare("INSERT INTO usuarios (nome, telefone, email, senha) VALUES (:n, :t, :e, :s)");

         $sql->bindValue(":n",$nome);
         $sql->bindValue(":t",$telefone);
         $sql->bindValue(":e",$email);
         $sql->bindValue(":s",md5($senha));
         $sql->execute();
         return true;



       }


   }


   public function logar($email, $senha)
   {

       global $pdo;
       $sql = $pdo->prepare("SELECT id_usuarios FROM usuarios WHERE email = :e AND senha = :s");
       $sql->bindValue(":e", $email);
       $sql->bindValue(":s", md5($senha));
       $sql->execute();
       if($sql->rowCount() > 0)
       {
          $dado = $sql->fetch();
          session_start();
          $_SESSION['id_usuarios'] = $dado['id_usuarios'];
          return true;
       }
       else
       {
         return false;
       }

   }

}

?>

  • Welcome Sorrayla, put in question the way you are making the call of this class.

  • Your $Pdo variable is null ....

1 answer

-1

I believe your problem is that overall when you are in a class you do not use it to refer to the $Pdo attribute you declare you should use $this->So this goes for all attributes and method calls within the class I’ll leave an example below for you to view better

public function conectar($nome, $host, $email, $senha)
{
  try
  {
      $this->pdo = new PDO("mysql:dbname=".$nome.";host".$host,);
   } catch (PDOException $e) {
      $msgErro = $e->getMessage(); 

   }


}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.