[英]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.