“注意:php中的未定义属性:stdClass :: $ id”

时间:2016-06-03 18:25:24

标签: php function session

我在以下函数的Notice: Undefined property: stdClass::$id行和$uid = $data->id;行中获得了$uname = $data->username;

public function userlogin()
{
    $sql = 'select id, username from login_user where email="'.$this->email.'"and password="'.$this->password.'"';
    $result = mysqli_query($this->cn,$sql);
    $numrows = mysqli_num_rows($result);
    if($numrows == 1)
    {
        $data = mysqli_fetch_field($result);
        $uid = $data->id;
        $uname = $data->username;

        $_SESSION['login'] = 1;
        $_SESSION['uid'] = $uid;
        $_SESSION['uname'] = $uname;
        $_SESSION['login_msg'] = 'Login Successfully...';

    }

}

1 个答案:

答案 0 :(得分:0)

你得到的原因

  

注意:未定义的属性:stdClass :: $ id

是因为mysqli_fetch_field没有返回具有id属性的对象。它用于获取有关列的元信息。您可以在document page上看到完整的属性列表。

您可能希望使用mysqli_fetch_object

public function userlogin()
{
    $sql = 'select id, username from login_user where email="'.$this->email.'"and password="'.$this->password.'"';
    $result = mysqli_query($this->cn,$sql);
    $numrows = mysqli_num_rows($result);
    if($numrows == 1)
    {

        /* fetch object array */
        while ($data = mysqli_fetch_object($result)) {
          $uid = $data->id;
          $uname = $data->username;
        }

        /* free result set */
        mysqli_free_result($result);

        $_SESSION['login'] = 1;
        $_SESSION['uid'] = $uid;
        $_SESSION['uname'] = $uname;
        $_SESSION['login_msg'] = 'Login Successfully...';

    }

}