如果我更改主机,我会在本地ampps主机中收到一个错误,我现在没有任何问题,尽管我看到的这个错误是相同的php版本是7.1,而本地版本是7.1,但是为什么我会收到此错误? 这是错误代码:
Fatal error: Uncaught Error: Call to a member function userDetails() on null in /home/root/requests/index.php:14 Stack trace: #0 {main} thrown in /home/root/requests/index.php on line 14
这是我在index.php顶部的功能
<?php
include('config.php');
include('session.php');
$userDetails=$userClass->userDetails($session_uid);
这是我的session.php代码
<?php
if(!empty($_SESSION['uid']))
{
$session_uid=$_SESSION['uid'];
include('userClass.php');
$userClass = new userClass();
}
if(empty($session_uid))
{
$url=BASE_URL.'/login.php';
header("Location: $url");
}
?>
这是我的userClass.php代码:
<?php
class userClass
{
/* User Login */
public function userLogin($usernameEmail,$password)
{
try{
$db = getDB();
$hash_password= hash('sha256', $password); //Password encryption
$stmt = $db->prepare("SELECT uid FROM users WHERE (username=:usernameEmail or email=:usernameEmail) AND password=:hash_password");
$stmt->bindParam("usernameEmail", $usernameEmail,PDO::PARAM_STR) ;
$stmt->bindParam("hash_password", $hash_password,PDO::PARAM_STR) ;
$stmt->execute();
$count=$stmt->rowCount();
$data=$stmt->fetch(PDO::FETCH_OBJ);
$db = null;
if($count)
{
$_SESSION['uid']=$data->uid; // Storing user session value
return true;
}
else
{
return false;
}
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
/* User Details */
public function userDetails($uid)
{
try{
$db = getDB();
$stmt = $db->prepare("SELECT email,username,name FROM users WHERE uid=:uid");
$stmt->bindParam("uid", $uid,PDO::PARAM_INT);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_OBJ); //User data
return $data;
}
catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
}
?>
答案 0 :(得分:0)
您是否定义了$userClass
,如果没有定义,则应使一个对象成为您在此处使用的userClass,然后在该类内调用该方法,它将起作用。