4

可能重复:
CodeIgniter - 在非对象上调用成员函数 select()

我是codeigniter的新手,遇到了一些问题。

错误消息: 致命错误:调用第 12 行 C:\xampp\htdocs\moi\CI\application\models\model_users.php 中非对象的成员函数 where()

我的模型:

class Model_users extends CI_Model {

 function __construct(){
    parent::__construct();
}

public function can_log_in() {


    $this->db->where('email', $this->input->post('email'));
    $this->db->where('pass', md5($this->input->post('password')));
    $query = $this->db->get('users');

    if ($query->num_rows()==1) {
        return TRUE;
    } else {
        return FALSE;
    }

我的控制器:

class Main extends CI_Controller {

public function index() {
    $this->login();
}

//Auslagern der Funktionen
public function login() {
    $this->load->view('login');
}

public function login_validation() {

    $this->load->library('form_validation');

    $this->form_validation->set_rules('email', 'email', 'required|valid_email|xss_clean|callback_username_check');
    $this->form_validation->set_rules('password', 'password', 'required|md5');

    if ($this->form_validation->run()) {
        redirect('main/members');
    } else {
        $this->load->view('login');
    }
}

public function username_check() {
    $this->load->library('form_validation');
    $this->load->model('model_users');
    if ($this->model_users->can_log_in()) {
        return TRUE;
    } else {
        $this->form_validation->set_message('username_check', 'incorect User or Passwort.');
        return FALSE;
    }
}

}

请帮忙

4

4 回答 4

1

See below URL

CodeIgniter - Call to a member function select() on a non-object

Try it

class User_model extends CI_Model { 

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

     public function can_log_in() {


        $this->db->select('*');
        $this->db->from('user');
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $validate_user = $this->db->get();

       if ($query->num_rows()==1) {
         return TRUE;
       } else {
         return FALSE;
      }


}

}
于 2012-10-11T19:50:00.197 回答
1

$this->db似乎没有定义。似乎您的模型中没有属性名称$db

你用过:$this->load->database();来初始化你的数据库吗?

试试下面的代码:

class Model_users extends CI_Model {

    function __construct(){
        parent::__construct();
        $this->load->database();
    }

信息
示例: http: //maestric.com/doc/php/codeigniter_models
用户指南:http ://ellislab.com/codeigniter/user_guide/database/examples.html

于 2012-10-11T19:48:24.067 回答
0

The problem you have is not that much related to Codeigniter in specific nor to the database.

It is first of all a standard PHP error, for the general info please see

In your cases - just forget Codeigniter for a moment - this means that $this does bot have any ->db or ->load member, lines of code that fail:

$this->db->where('email', $this->input->post('email'));
$this->load->database();

It's obvious that this needs further debugging. Why aren't those libraries loaded? Why isn't it even possible to load the database?

Well actually this requires further debugging. It makes no sense in my eyes that you ask in comments further on, the other users can only give as good answers as you provide good information in your question. But asking a question is always limited as well, sure.

Maybe first of all post the complete code of your model class. It might just be the model class itself already screws it. Something a fresh pair of eyes here is faster to spot.

But there is no guarantee for that, just a possibility. You can break that also in other places. These are hard to reach, so you best do that using a step-debugger like Xdebug in your development environment.

于 2012-10-12T19:57:29.060 回答
0

你有自动加载的数据库吗?如果没有,您可能需要将此代码添加到构造函数中:

$this->load->database();
于 2012-10-11T19:46:41.660 回答