Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP致命错误:未捕获错误:在null上调用成员函数prepare()_Php_Mysql - Fatal编程技术网

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

PHP致命错误:未捕获错误:在null上调用成员函数prepare(),php,mysql,Php,Mysql,我的PHP文件中出现了这个错误 我的登录功能 <?php session_start(); include ("../dbConnection.php"); class login { public $link; function __construct() { $dbc = new dbConnection(); $this->link = $dbc->Connect(); return $thi

我的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;
        }            
    }
}
错误


我不知道这有什么问题。如果有人知道解决方案,请帮助我解决这个问题。提前谢谢。

替换这部分代码

$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));
定义类时,确实需要构造函数来初始化实例变量

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 = '';
    }

    /* ... */
此外,类不能定义为公共类,这将在编译时引发语法错误

所以您只需要类dbConnection{

我不确定这是否会导致问题,但构造函数不能返回值。事实上,他们返回刚刚创建的实例对象

您已登录到类中:

最后这个

    $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'");

php中可能重复的构造函数不能返回任何东西!我尝试过,但没有解决我的问题。这不是一个准备好的语句,您只是直接在sql语句中注入变量,就像用撬棍敲打一个nail@arkascha你能支持你的评论吗?不是我的反对票。但是你指出的问题t只是其中的一个,而不是破坏代码的那个。
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 = '';
    }

    /* ... */
function __construct()
{
    $dbc = new dbConnection();
    $this->link = $dbc->Connect();
    // return $this->link;  // <--- REMOVE THAT - YOU CANNOT RETURN VALUES HERE!
}
$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'");