无法在PHP上获得变量

时间:2016-10-13 14:43:54

标签: php

喂!
我试图理解为什么这段代码不起作用:

public static function selectUser($connection, $userid){

        require_once 'UserRepository.inc.php';

        if(!isset($connection) || !isset($userid)){
            return;
        }

        $result = mysqli_query($connection, "SELECT * FROM users WHERE id = ".$userid."");
        $row = mysqli_fetch_assoc($result);

        $user = new User($userid, 
                $row["email"], 
                $row["cp_username"],
                $row["mc_username"],
                $row["forum_username"],
                $row["password"],
                $row["avatarurl"]);

        return $user;


    }

基本上,这应该做的是当你创建一个变量($user = UserMan::selectUser(Connection::getConnection(), $_COOKIE["uid"]))并尝试召唤一个get数据属性($user->getCpUsername())时,它不起作用。现在我的页面看起来像这样:
Image of how my page looks like right now
我需要在那里打印名称。我不确定我做错了什么 谢谢你耐心等待。祝你有美好的一天!

<小时/> 编辑:

    class User {

    private $id;
    private $email;
    private $cp_username;
    private $mc_username;
    private $forum_username;
    private $password;
    private $avatarurl;

    public function __construct($id, $email, $cp_username, $mc_username, $forum_username, $password, $avatarurl) {

        $this->id = $id;
        $this->email = $email;
        $this->cp_username = $cp_username;
        $this->mc_username = $mc_username;
        $this->forum_username = $forum_username;
        $this->password = $password;
        $this->avatarurl = $avatarurl;

    }

    public function getId(){
        return $this->id;
    }
    public function getEmail(){
        return $this->email;
    }
    public function getCpUsername(){
        return $this->cp_username;
    }
    public function getMcUsername(){
        return $this->mc_username;
    }
    public function getForumUsername(){
        return $this->forum_username;
    }
    public function getPassword(){
        return $this->password;
    }
    public function getAvatarurl(){
        return $this->avatarurl;
    }
    public function setEmail($email){
        $this->email = $email;
    }
    public function setCpUsername($username){
        $this->cp_username = $username;
    }
    public function setMcUsername($username){
        $this->mc_username = $username;
    }
    public function setForumUsername($username){
        $this->forum_username = $username;
    }
    public function setPassword($password){
        $this->password = $password;
    }
    public function setAvatarurl($avatarurl){
        $this->avatarurl = $avatarurl;
    }

}

1 个答案:

答案 0 :(得分:0)

尝试更改

$row = mysqli_fetch_assoc($result);

$row = mysqli_fetch_assoc($result)[0];