4

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

I'm newbie with codeigniter and have some problems.

Error Message: Fatal error: Call to a member function where() on 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;
    }
}

}

please for help

Community
  • 1
  • 1
user1739222
  • 89
  • 1
  • 6
  • Apprently the method `where` was called on something that is not an object. Can you show user the user superclass? – thatidiotguy Oct 11 '12 at 19:42
  • but is defined in my config file – user1739222 Oct 11 '12 at 19:44
  • I do not know code igniter, but the error is clear. `$this->db` is not an object. You are doing something wrong, if you think if you request the db field from the object's superclass. – thatidiotguy Oct 11 '12 at 19:45
  • how I can manage this problem? – user1739222 Oct 11 '12 at 19:47
  • Try to change the line `$this->load->model('model_users'); ` in `username_check()` to `$this->load->model('model_users', '', TRUE);` to autoconnect to your database – halex Oct 11 '12 at 19:50
  • Seems like you turned off database debugging. Could you please add you database.php configuration file (without the password ofc). – Repox Oct 11 '12 at 20:13
  • `$active_group = 'default'; $active_record = TRUE; $db['default']['hostname'] ='localhost'; $db['default']['username'] ='test'; $db['default']['password'] ='test'; $db['default']['database'] ='ci2'; $db['default']['dbdriver'] ='mysql'; $db['default']['dbprefix'] = ''; $db['default']['pconnect'] = TRUE; $db['default']['db_debug'] = TRUE; $db['default']['cache_on'] = FALSE; $db['default']['cachedir'] = ''; $db['default']['char_set'] = 'utf8'; $db['default']['dbcollat'] = 'utf8_general_ci'; $db['default']['swap_pre'] = ''; $db['default']['autoinit'] = TRUE; $db['default']['stricton'] = FALSE;` – user1739222 Oct 11 '12 at 20:17
  • Seems like something else is wrong - it should work. And if something was wrong, you should be notified whenever you load the database. – Repox Oct 11 '12 at 20:33
  • I've no idea why it does not work – user1739222 Oct 11 '12 at 20:36
  • Well, you need to start some debugging - try var_dump() on $this->db on strategic places and see what you get. – Repox Oct 11 '12 at 21:15
  • did you autoload the database? – Muhammad Raheel Oct 12 '12 at 05:47

4 Answers4

1

$this->db seems not to be defined. Seems you dont have a property names $db within your Model.

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

Try 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

Kumar V
  • 8,810
  • 9
  • 39
  • 58
Niels
  • 48,601
  • 4
  • 62
  • 81
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;
      }


}

}
Community
  • 1
  • 1
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • `$autoload['libraries'] = array('database');` -->application/config/autoload.php – user1739222 Oct 11 '12 at 20:00
  • i have update my answer please take a look. – Abid Hussain Oct 11 '12 at 20:08
  • Fatal error: Call to a member function database() on a non-object in C:\xampp\htdocs\moi\CI\application\models\model_users.php on line 7 – user1739222 Oct 11 '12 at 20:12
  • without // $this->load->database(); i got this Fatal error: Call to a member function select() on a non-object in C:\xampp\htdocs\moi\CI\application\models\model_users.php on line 13 – user1739222 Oct 11 '12 at 20:13
  • see this URL http://stackoverflow.com/questions/8322124/codeigniter-call-to-a-member-function-select-on-a-non-object – Abid Hussain Oct 11 '12 at 20:15
  • **Problem solved! I just add word 'database' in autoload.php file. $autoload['libraries'] = array('database');** but on my server is the database autoloaded – user1739222 Oct 11 '12 at 20:21
0

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

$this->load->database();
Jason
  • 1,192
  • 7
  • 8
  • I got similar error message : Fatal error: Call to a member function database() on a non-object in C:\xampp\htdocs\moi\CI\application\models\model_users.php on line 7 – user1739222 Oct 11 '12 at 20:02
  • You didn't by chance move or rename the Codeigniter system folder did you? It really sounds like none of the CI core is loading right. – Rick Calder Oct 11 '12 at 22:47
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.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836