Fatal error: calling member function where () for non-object codeigniter $ query-> num_rows () == 1)

Possible duplicate:
CodeIgniter - call the select () member function for a non-object

I am new to codeigniter and have some problems.

Error message: Fatal error: call the member function where () for a non-object in C: \ xampp \ htdocs \ moi \ CI \ application \ models \ model_users. php on line 12

My model:

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

My controller:

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

request for help

+4
source share
4 answers

$this->db does not seem to be defined. You don't seem to have $db property names in your model.

You used: $this->load->database(); to initialize your database?

Try using the following code:

 class Model_users extends CI_Model { function __construct(){ parent::__construct(); $this->load->database(); } 

Information
Example: http://maestric.com/doc/php/codeigniter_models
User Guide: http://ellislab.com/codeigniter/user_guide/database/examples.html

+1
source

See URL below

CodeIgniter - call the select () member function for a non-object

Try

 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; } } } 
+1
source

Do you have your database at startup? If not, you probably need to add this code to the constructor:

 $this->load->database(); 
0
source

The problem you are facing is not so closely related to Codeigniter in the specific and in the database.

This is, first of all, a standard PHP error, for general information see

In your cases - just forget Codeigniter for a moment - this means that $this has a bot with any member ->db or ->load , lines with code that don't execute:

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

Obviously, this requires further debugging. Why are these libraries not loading? Why can't download database?

Well, actually it requires further debugging. In my eyes there is no point in asking in the comments, other users can give only good answers, since you provide good information in your question. But asking a question is always limited, of course.

Perhaps, first of all, post the full code of your model class. It may just be the model class itself, which is already spinning it. Something a fresh pair of eyes here is more quickly detected.

But there is no guarantee for this, just an opportunity. You can break it in other places. They are difficult to achieve, so it’s best for you to do this with a debugger like Xdebug in your development environment.

0
source

Source: https://habr.com/ru/post/1439244/


All Articles