简体   繁体   English

致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ oophp \\ Home.php中调用成员函数connect()时为null

[英]Fatal error: Uncaught Error: Call to a member function connect() on null in C:\xampp\htdocs\oophp\Home.php

I tried linking my database with my php code and It got completed but tried in sending information to my database but it returns the fallowing error I tried it by creating an object and calling the method of the class but it returned me the error in the line I think there is a problem in calling the function but could not understand the error and even tried googling but could not solve the issue 我尝试将数据库与php代码链接,并完成了操作,但是尝试将信息发送到数据库,但是它返回了错误的错误,我通过创建对象并调用类的方法尝试了此错误,但是它在行中返回了错误我认为调用该函数存在问题,但无法理解错误,甚至尝试使用谷歌搜索,但无法解决问题

Fatal error: Uncaught Error: Call to a member function connect() on null in C:\\xampp\\htdocs\\oophp\\Home.php:15 Stack trace: #0 C:\\xampp\\htdocs\\oophp\\Home.php(47): Home->__construct('example', 'example123', 'example8@gmail....') #1 {main} thrown in C:\\xampp\\htdocs\\oophp\\Home.php on line 15 致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ oophp \\ Home.php:15中调用null的成员函数connect()堆栈跟踪:#0 C:\\ xampp \\ htdocs \\ oophp \\ Home.php(47 ):Home-> __ construct('example','example123','example8 @ gmail ....')#1 {main}放在第15行的C:\\ xampp \\ htdocs \\ oophp \\ Home.php中

and the code in Connect.php is 并且Connect.php中的代码是

<?php
class Connect
{

protected $connection;

public function __construct()
{

}
public function connect(){
    try
    {
        $this->connection = new 
PDO('mysql:host=localhost;dbname=oo;charset=utf8mb4','root','');
        echo "connected sucessfully";
        return $this->connection;
    }
    catch(PDOException $e)
    {
        echo "connection failed : " . $e->getMessage();
    }
}
} 
?>

and code in Home.php is 和Home.php中的代码是

<?php
require_once __DIR__ . '\Connect.php'; 
class Home extends Connect{
protected $user = array();
protected $var0;
protected $response = array();
protected $username;
protected $password;
protected $email;
protected $var1;

public function __construct($username , $password ,$email)
{
    $var0 = new Connect();
    $var1 = $this->var0->connect();
    $this->username = $username;
    $this->password = $password;
    $this->email = $email;
}
public function Insert()
{
    try
    {
        global $var1;
        $insert = $this->var1->prepare("INSERT INTO person 
        (username,password,email) VALUES (:username,:password,:email)");
        $insert->bindParam(':username',$this->username);
        $insert->bindParam(':password',$this->password);
        $insert->bindParam(':email',$this->email);
        $insert->execute();
    }
    catch(PDOException $e)
    {
        echo $e;
    }
        if(row_Count($insert)>0)
        {
            $this->response["status"] = 1;
            echo "Sucessfully inserted to database";
        }
        else
        {
            $this->response["status"] = 0;
            echo "there is a error in inserting";
}
}
}
$dfg  = new Home("example","example123","example8@gmail.com");
$dfg->Insert();
?>

I can't find the issue but I think there is an error at connect() function, thanks for any help 我找不到问题,但我认为connect()函数存在错误,感谢您的帮助

change your __construct and remove global $var1; 更改您的__construct并删除global $var1;

public function __construct($username , $password ,$email)
{
    $this->var0 = new Connect();
    $this->var1 = $this->var0->connect();
    $this->username = $username;
    $this->password = $password;
    $this->email = $email;
}

暂无
暂无

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

相关问题 Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php - Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php 致命错误:未捕获的错误:调用成员函数 prepare() 在 null [C:\\xampp\\htdocs\\twitter\\core\\classes\\user.php on line 19] - Fatal error: Uncaught Error: Call to a member function prepare() on null [C:\xampp\htdocs\twitter\core\classes\user.php on line 19] 致命错误:未捕获错误:调用 function sqlsrv_connect() in C:\xampp\htdocs\DBtest\sqlsrv.php:7 堆栈跟踪:#0 {main} SQLSERVER - Fatal error: Uncaught Error: Call to function sqlsrv_connect() in C:\xampp\htdocs\DBtest\sqlsrv.php:7 Stack trace: #0 {main} SQLSERVER 致命错误:未捕获错误:调用 C:\\xampp\\htdocs\\ 中未定义的函数 mysql_connect() - Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\ 致命错误:在第42行上,在C:\\ xampp \\ htdocs \\ website \\ adduser.php中的布尔值上调用成员函数execute() - Fatal error: Call to a member function execute() on boolean in C:\xampp\htdocs\website\adduser.php on line 42 致命错误:在第 30 行调用 C:\\xampp\\htdocs\\magento\\test.php 中布尔值的成员函数 getChildren()? - Fatal error: Call to a member function getChildren() on boolean in C:\xampp\htdocs\magento\test.php on line 30? 致命错误:在C:\\ xampp \\ htdocs \\ index.php中以布尔值调用成员函数bind_param() - Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\index.php 致命错误:在第 16 行调用 C:\\xampp\\htdocs\\studysite\\test.php 中布尔值的成员函数 fetchAll() - Fatal error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\studysite\test.php on line 16 致命错误:未捕获错误:调用 C:\\xampp\\htdocs\\phpmvc\\app\\models\\Mahasiswa_model.php:31 中未定义的函数 query() - Fatal error: Uncaught Error: Call to undefined function query() in C:\xampp\htdocs\phpmvc\app\models\Mahasiswa_model.php:31 致命错误:在第3行的C:\\ xampp \\ htdocs \\ Project1 \\ connect.php中调用未定义函数oci_connect() - Fatal error: Call to undefined function oci_connect() in C:\xampp\htdocs\Project1\connect.php on line 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM