gpt4 book ai didi

php - 对 null 调用成员函数prepare()

转载 作者:行者123 更新时间:2023-11-29 21:15:44 28 4
gpt4 key购买 nike

我的登录页面代码也有同样的问题。

Call to a member function prepare() on null

我有我的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

<?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 连接到数据库。

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

我试图纠正这个错误,但错误是在登录方法和注册中...我已经阅读了解决方案,但我应该声明对象 PDO 并且我应该初始化该对象...

最佳答案

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

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。因此你会得到这个错误。

关于php - 对 null 调用成员函数prepare(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35959311/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com