繁体   English   中英

PHP致命错误:未捕获错误:在null上调用成员函数prepare()

[英]PHP Fatal error: Uncaught Error: Call to a member function prepare() on null

我的PHP文件中出现此错误。

我的登录功能

<?php
session_start();
include ("../dbConnection.php");


class login {

    public $link;

    function __construct()
    {
        $dbc = new dbConnection();
        $this->link = $dbc->Connect();
        return $this->link;
    }

    public function get_data($emailid,$password)
    {
        $q = $this->link->prepare("SELECT id from students WHERE emailid='$emailid' AND password='$password' AND active='Yes'");
        $q->execute(array(':emailid'=>$emailid,':password'=>$password));
        $counts = $q->fetch();
        if($counts['id'] > 0)
        {
            session_start();
            $_SESSION['userlogin'] = $counts['id'];
            $encrypt_id1 = $this->encrypt_decrypt('encrypt', $counts['id']);
            echo $encrypt_id1;
        }            
    }
}

dbConnection.php

<?php

public class dbConnection {

    public $conn;


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

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

我在以下声明中遇到错误

"$q = $this->link->prepare("SELECT id from students WHERE emailid='$emailid' AND password='$password' AND active='Yes'");"

错误

"PHP Fatal error:  Uncaught Error: Call to a member function prepare() on null"

我不知道这是怎么回事。如果有人知道解决方案,请帮助我摆脱这个问题。谢谢。

当定义一个类时,确实需要一个构造函数来初始化实例变量。

class dbConnection {

    public $conn,

           $db_host,
           $db_name,
           $db_user,
           $db_pass;

    public function __construct()
    {
        $this->conn = false;

        $this->db_host = 'localhost';
        $this->db_name = 'pte_mock';
        $this->db_user = 'root';
        $this->db_pass = '';
    }

    /* ... */

同样,不能将class定义为public ,这会在编译时引发语法错误。

因此,您只有class dbConnection {


我不确定这是否会导致问题,但委托人一定不能返回值。 实际上,它们返回刚刚创建的实例(对象)

您已经进入班级login

function __construct()
{
    $dbc = new dbConnection();
    $this->link = $dbc->Connect();
    // return $this->link;  // <--- REMOVE THAT - YOU CANNOT RETURN VALUES HERE!
}

当您调用$lgn = new login()将调用__construct()函数,并在$lgn获得一个类login的新实例。 如果构造函数返回任何被丢弃的东西!

因此,您应该通过以下方式重构代码:

$lgn = new login(); // <--- returns a new instance of the class login
$the_link= $lgn->link; // <--- this way you access `link` instance variable 

最后这个

    $q = $this->link->prepare("SELECT id from students WHERE emailid='$emailid' AND password='$password' AND active='Yes'");

在将值注入查询中而不是指定占位符时,这不是构建预准备语句的正确方法。

该行应该这样写

    $q = $this->link->prepare("SELECT id from students WHERE emailid=:$emailid AND password=:password AND active='Yes'");

替换这部分代码

$q = $this->link->prepare("SELECT id from students WHERE emailid='$emailid' AND password='$password' AND active='Yes'");

$q->execute(array(':emailid'=>$emailid,':password'=>$password));

有了这个:

$q = $this->link->prepare("SELECT id FROM students WHERE emailid = :emailid AND password = :password AND active = 'Yes'");

$q->execute(array(':emailid'=>$emailid,':password'=>$password));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM