Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 539
softboxkid Avatar asked Nov 30 '11 06:11

softboxkid


2 Answers

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

like image 168
Vasil Dakov Avatar answered Oct 14 '22 19:10

Vasil Dakov


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();
        }
    }
}
like image 44
Andre Dublin Avatar answered Oct 14 '22 21:10

Andre Dublin