在..中的非对象上调用成员函数where()第13行


Call to a member function where() on a non-object in ... on line 13

我是CodeIgniter的新用户,我正在尝试使用CodeIgniter创建简单的登录页面,但我收到以下错误:

调用成员函数where()在C:'xampp'htdocs'CodeIg'application'models'user.php第13行

我不确定从哪里开始调试这个,建议将不胜感激。

模型代码如下:

<?php
class User extends CI_model{  
    function __construct()
    {
        parent::__construct();
    }
    public function verifyuser()
    {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $remember = $this->input->post('remember'); 
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('user_login');
        $result = array();
        if($query->num_rows==0)
        {
            $result['false']=false;
            return $result;
        }
        else
        {
            $result=$query->result();
            foreach($result as $item)
            {
                $result['id']=$item->id;
                $result['username']=$item->username;
                $result['password']=$item->password;
            }
            return $result;
        }
    }
}
?>

,控制器代码为:

<?php
    class User_Controller extends CI_controller
    {
        public function getloginData()
        {
            $this->load->model('User');
            $rvalue = $this->User->verifyuser();
            header('Content-type: application/json');
            echo json_encode($rvalue);
        }
    }
?>

数据库未初始化。
你需要在你的应用程序/config/autolload .php:

中包含"database"
$autoload['libraries'] = array('database', 'session');

或者在你的模型类构造函数中:

class User extends CI_model{ 
     public function __construct() 
     {
           parent::__construct(); 
           $this->load->database();
     }
}

您可以在这里获得更多信息:http://ellislab.com/codeigniter/user_guide/database/connecting.html

$this->db未初始化,因此$this->db为null,并且在null上调用where()导致此错误。你必须先给$this->db赋值。

要做到这一点,你必须在你的控制器
中加载模型。