我这里有问题, 当我上这堂课的时候 类属性即使在初始化后仍为NULL
class User
{
public $name;
public $id;
public $email;
private $password;
private $dbconn;
function __construct()
{
$this->dbconn= new Config\Database();
}
public function login( $email, $password){
$matches=$this->dbconn->query("SELECT pwhash from users WHERE email = '$email'");
if($matches->num_rows==0){
return "no such user";
}else{
while($row =$matches->fetch_assoc()){
if($row['pwhash']==hash("sha256",$password)){
$this->name=$row['name'];
$this->id=$row['id'];
return "login successful";
}else{
return "login fail";
}
}
}
}
我尝试访问php文件之外的类,
$user = new User\User();
if(isset($_POST['email']) && isset($_POST['password']) ){
$loginmessage=$user->login($_POST['email'],$_POST['password']);
}
if($loginmessage=="login successful"){
$userarray= get_object_vars($user);
var_dump($userarray);
}
然后我显示
的var_dump$userarray
["name"]=>
NULL
["id"]=>
NULL
背后的原因是什么?
答案 0 :(得分:3)
您没有选择' name'或者' id'在你的SQL中,所以你在使用时得到NULL:
$this->name=$row['name'];
$this->id=$row['id'];
将您的SQL更改为:
$matches=$this->dbconn->query("SELECT pwhash,name,id from users WHERE email = '$email'");