14

I'm quite new to CodeIgniter. This is my code:

class User_model extends CI_Model {

    function validate_user() {

        $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($validate_user->num_rows == 1) {
            return TRUE;
        }
    }
}

I'm, receiving this error in my model file:

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

Currently I'm using CodeIgniter version 2.1.0. Please help me!

George Brighton
  • 5,131
  • 9
  • 27
  • 36
softboxkid
  • 877
  • 6
  • 13
  • 31
  • possible duplicate of [Codeigniter - I am looking to use/connect to a different database for one of my controllers and one model](http://stackoverflow.com/questions/312511/codeigniter-i-am-looking-to-use-connect-to-a-different-database-for-one-of-my) – tereško Feb 12 '13 at 21:45
  • 3
    This is a very helpful thread that shows up high in the search results when searching this particular error. Please stop being so quick to cry duplicate. – ChuckKelly Jul 27 '13 at 06:21

3 Answers3

44

I think that you have to load the "database" library. The first way is to include the "database" in your application/config/autoload.php

$autoload['libraries'] = array('database', 'session');

or in your class constructor:

class User_model extends CI_Model { 

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

You can get more information here: https://www.codeigniter.com/user_guide/database/connecting.html

Stack Programmer
  • 679
  • 6
  • 18
Vasil Dakov
  • 2,040
  • 2
  • 19
  • 38
4

It looks like your not sticking to the MVC pattern. You should be passing the data from view -> controller -> model.

As for sending information to the database, I'm pretty sure that CI handles xss and filter input, but you can never be to sure.

Also make sure you are loading your models in the config/autoload.php file or initiate the model in the controller __construct() function

<?php
    class User extends CI_Controller
    {
        public __construct()
        {
            parent::__construct();
            $this->load->model('User_model');
        }
    }

or

$autoload['model'] = array('User_model');

So for example in my login view, I would have the CI create the fields needed.

<?php 
    echo form_open('admin');
    echo form_label('Username:', 'username');
    echo form_input('username', 'name');
    echo form_label('Password:', 'password');
    echo form_password('password');
    echo form_submit('submit', 'Login', 'id="loginBtn"'); ?>
    echo form_close(); 
?>

Now in the controller

<?php
class User extends CI_Controller
{

    public function index()
    {

        $this->load->model('User_model');
        $result = $this
                    ->user_model
                    ->index(
                        $this->input->post('username'),
                        $this->input->post('password'));
    }

}
?>

And the model

<?php

class User_model extends CI_Model
{


    function index($username, $password)
    {
        $q = $this
                ->db
                ->where('username', $username)
                ->where('password', md5($password))
                ->limit(1)
                ->get('user');

        if ($q->num_rows() > 0) {
            return $q->row();
        }
    }
}
Andre Dublin
  • 1,148
  • 1
  • 16
  • 34
  • To update my answer, you should probably use sha256, and salt + password hashing, since md5 is not considered best practices anymore – Andre Dublin Jan 18 '12 at 12:46
0

num_rows is a function, thus you need to add ()

if($validate_user->num_rows() == 1) {
        return TRUE;
}

Also, are you calling the parent class constructor in the constructor

class User_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
}
Bogdan
  • 5,368
  • 9
  • 43
  • 62
  • Oh, it seems the db object is not initialized. Are you calling the parent class constructor in the constructor? – Bogdan Nov 30 '11 at 07:09
  • No. I'm not calling any constructor on my code. Even if I put the __construct() function, the error is still there saying that "Fatal error: Call to a member function where() on a non-object in C:\AppServ\www\ci\application\models\user_model.php on line 14" – softboxkid Nov 30 '11 at 07:19
  • 1
    Problem solved! I just add word 'database' in autoload.php file. $autoload['libraries'] = array('database'); – softboxkid Nov 30 '11 at 07:42
  • just to make things clear, the parent constructor will ALWAYS be called unless you override the __construct() function in your child-class. In this case you need to explicitly call the parent's constructor, with parent::__construct(); – mwallisch Nov 30 '11 at 09:09