-1

help me please .. whats the wrong in my code. I try to create login form and verified the input..

Fatal error: Uncaught Error: Call to a member function dbConnect() on null

( On $this->$db = $this->db->dbConnect();).

this is connection.php

<?php

class connection{

    public $db_host = 'localhost';
    public $db_name = 'login';
    public $db_user = 'root';
    public $db_pass = 'root';

    public function dbConnect()
    {
        try{
            $conn = new PDO("mysql:host=".$this->db_host.";dbname=".$this->db_name,$this->db_user,$this->db_pass);
           $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $e)
        {
            echo 'ERROR: ' . $e->getMessage();
        }    
        return $this->conn;
    }
}

?>

and this is login.php

<?php

include_once('connection.php');

class User{

    private $db;
    public function __construct(){

        $this->$db = new connection();
        $this->$db = $this->db->dbConnect();

    }

    public function isAuthenticated($name, $pass){

        if(!empty($name) && !empty($pass)){

            $st = $this->$db->prepare("SELECT * FROM loginUser WHERE username =? AND password =?");
            $st->bindParam(1, $name);
            $st->bindParam(2, $pass);
            $st->execute();

            if($st->rowCount() == 1){
                echo "User Verified";
            }else{
                echo "Incorrect UserName or Password";
            }

        }else{

            echo "Please Enter User Name And Password";
        }

    }



}

?>
tereško
  • 58,060
  • 25
  • 98
  • 150
Second View
  • 154
  • 9

1 Answers1

2

Replace this:

    $this->$db = new connection();
    $this->$db = $this->db->dbConnect();

with this:

    $this->db = (new connection())->dbConnect();

And this:

$this->$db->prepare

with this:

$this->db->prepare

Actually, in every place you should replace:

$this->$db

with

$this->db
Sergej
  • 2,030
  • 1
  • 18
  • 28