【问题标题】:Call to a member function where() on a non-object in ... on line 13在第 13 行调用 ... 中的非对象上的成员函数 where()
【发布时间】:2013-03-11 13:09:49
【问题描述】:

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

在第 13 行调用 C:\xampp\htdocs\CodeIg\application\models\user.php 中非对象的成员函数 where()

我不确定从哪里开始调试它,我们将不胜感激。

我的模型代码如下:

<?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);
        }
    }
?>

【问题讨论】:

    标签: php mysql json codeigniter


    【解决方案1】:

    数据库未初始化。
    您需要在 application/config/autoload.php 中包含“数据库”:

    $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

    【讨论】:

      【解决方案2】:

      $this-&gt;db 未初始化,因此$this-&gt;db 为 null 并且在 null 上调用 where() 会导致此错误。您必须先将某些内容分配给 $this->db。

      为此,您必须在控制器中加载模型

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-12
        • 2016-05-24
        • 2015-01-27
        • 2011-12-24
        • 2016-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多