Call to a member function prepare() on null [dupli

2020-02-16 03:53发布

问题:

I have same problems with my login page code.

Call to a member function prepare() on null

I have my index.php

<?php
if(!isset($_SESSION)) 
        session_start(); 
require_once("user.class.inc.php");
$login = new USER();
if($login->is_loggato()!="")
{
    $login->redirect('dashboard.php');
}
if(isset($_POST['btn-login']))
{
    $umail = strip_tags($_POST['login-mail']);
    $upass = strip_tags($_POST['login-pswd']);

    if($login->Login($umail,$upass))
    {
        $login->redirect('dashboard.php');
    }
    else
    {
        $error = "Hai sbagliato!! Pensaci...come sono mail e password?";
    }
}
?>
[...]

And I have the class user.class.inc.php

<?php

require_once('database.class.inc.php');

class USER
{   

    private $conn;

    public function __construct()
    {
        $database = new Database();
        $db = $database->ConnessioneDB();
        $this->conn = $db;
    }

    public function EseguiQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }

    public function Registrazione($uname,$umail,$upass)
    {
        try
        {
            $new_password = password_hash($upass, PASSWORD_DEFAULT);
            $token_code=md5(rand(0,1000));
            $stmt = $this->conn->prepare("INSERT INTO Utenti(GED_user_name,GED_user_email,GED_user_pswd,GED_token_code) VALUES(:uname, :umail, :upass, :utk)");

            $stmt->bindparam(":uname", $uname);
            $stmt->bindparam(":umail", $umail);
            $stmt->bindparam(":upass", $new_password);
            $stmt->bindparam(":utk", $token_code);      

            $stmt->execute();   

            return $stmt;   
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }               
    }


    public function Login($umail,$upass)
    {
        try
        {
            $stmt = $this->conn->prepare("SELECT GED_user_id, GED_user_name, GED_user_email, GED_user_pswd FROM Utenti WHERE GED_user_email=:umail ");
            $stmt->execute(array(':umail'=>$umail));
            $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
            if($stmt->rowCount() == 1)
            {
                if(password_verify($upass, $userRow['GED_user_pswd']))
                {
                    $_SESSION['user_session'] = $userRow['GED_user_id'];
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
        }
    }

database.class.inc.php is a simple class where I use PDO to connect to database.

<?php
class Database
{
    private $host = "localhost";
    private $nome_db = "GED";
    private $username = "root";
    private $password = "root";
    public $conn;

    public function ConnessioneDB()
    {

        $this->conn = null;
        try
        {
            $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->nome_db, $this->username,$this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conn->exec("SET NAMES utf8");

        }
        catch(PDOException $e)
        {
          echo  "<div id='error-top'>Si &egrave; verificato un errore di connessione.</div>";
        }
    }
}
?>

I have tried to correct this error, but the error is in Login method and Registrazione...I have read the solution yet, but I should declare the object PDO and I should that I have inizialize the object...

回答1:

In your public function ConnessioneDB() in the Database classs you need to return the connection

class Database
{
    private $host = "localhost";
    private $nome_db = "GED";
    private $username = "root";
    private $password = "root";
    public $conn;

    public function ConnessioneDB()
    {

        $this->conn = null;
        try
        {
            $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->nome_db, $this->username,$this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->conn->exec("SET NAMES utf8");

            return $this->conn;


        }
        catch(PDOException $e)
        {
          echo  "<div id='error-top'>Si &egrave; verificato un errore di connessione.</div>";
        }
    }
}

If you do not return the connection the $conn property in your User class will be null. Thus you get this error.