在null上调用成员函数prepare() [英] Call to a member function prepare() on null

查看:120
本文介绍了在null上调用成员函数prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的登录页面代码有相同的问题.

I have same problems with my login page code.

Call to a member function prepare() on null

我有我的index.php

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?";
    }
}
?>
[...]

我有类user.class.inc.php

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是一个简单的类,在其中我使用PDO连接到数据库.

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>";
        }
    }
}
?>

我已尝试纠正此错误,但是该错误是Login方法和Registrazione中的问题...我已经阅读了解决方案,但是我应该声明对象PDO,并且应该使该对象伊斯兰化...

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...

推荐答案

在数据库类的公共函数ConnessioneDB()中,您需要返回连接

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>";
        }
    }
}

如果不返回连接,则User类中的$ conn属性将为null.因此,您会收到此错误.

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

这篇关于在null上调用成员函数prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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