简体   繁体   English

PHP-访问对象属性$ id时出错

[英]PHP - Error when accessing object property $id

I am following a tutorial about how to make register form with database connection but for some reason the code does not work for me. 我正在关注有关如何通过数据库连接创建注册表单的教程,但是由于某些原因,该代码对我不起作用。 I do not know what I am doing wrong so maybe someone can point it out for me. 我不知道我在做什么错,所以也许有人可以为我指出。

This is the code on the User.php file: 这是User.php文件上的代码:

<?php
class User {
    private $_db,
            $_data,
            $_sessionName;

    public function __construct($user = null) {
        $this->_db = DB::getInstance();

        $this->_sessionName = Config::get('session/session_name');
    }

    public function create($fields = array()) {
        if(!$this->_db->insert('users', $fields)) {
            throw new Exception('There was a problem creating an account.');
        }
    }

    public function find($user = null) {
        if($user) {
            $field = (is_numeric($user)) ? 'id' : 'username';
            $data = $this->_db->get('users', array($field, '=', $user));

            if($data->count()) {
                $this->_data = $data->first();
                return true;
            }
        }
        return false;
    }

    public function login($username = null, $password = null) {
        $user = $this->find($username);

        if($user) {
            if($this->data()->password === Hash::make($password, $this->data()->salt)) {
                Session::put($this->_sessionName, $this->data()->id);
                return true;
            }
        }
        return false;
    }

    private function data() {
        return $this->_data;
    }
}
?>

Getting this error back: 找回此错误:

Notice: Undefined property: stdClass::$id in /opt/lampp/htdocs/rd-website/classes/User.php on line 37 注意:第37行的/opt/lampp/htdocs/rd-website/classes/User.php中的未定义属性:stdClass :: $ id

Line 37 is: Session::put($this->_sessionName, $this->data()->id); 第37行是: Session::put($this->_sessionName, $this->data()->id);

Any help? 有什么帮助吗?

The problem was that i had the column named "ID" in the users table in the database, while in the User.php file it was written as "id". 问题是我在数据库的用户表中有名为“ ID”的列,而在User.php文件中它被写为“ id”。

Since Mysql is case insensitive if you are running on Windows that wouldn't be a problem or give any errors back, but for some Unix systems it's case sensitive, I run Kali-Linux so that was giving a error back. 由于如果您在Windows上运行时Mysql不区分大小写,这不会出现问题或返回任何错误,但是对于某些Unix系统,它是区分大小写的,因此我运行Kali-Linux以便返回错误。

I just changed the column "ID" to "id" in the database, as it was the easiest thing to do for this time and now the code works perfectly. 我只是将数据库中的“ ID”列更改为“ id”,因为这是目前最简单的方法,现在代码可以完美运行了。

If you are looking for a permanent solution then you would have to change the "my.cnf" file, 如果您正在寻找永久解决方案,则必须更改“ my.cnf”文件,

under LAMPP/XAMPP... : 在LAMPP / XAMPP ...下:

/opt/lampp/etc/my.cnf /opt/lampp/etc/my.cnf

stand alone mysql server : 独立的mysql服务器:

/etc/mysql/my.cnf /etc/mysql/my.cnf

with this line (lower_case_table_names = 1) You can change either to = 0,1 or 2 but that depends on your system and your needs. 使用这一行(lower_case_table_names = 1),您可以将其更改为= 0,1或2,但这取决于您的系统和需要。

After that you would need to restart the service again. 之后,您将需要重新启动服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM