Welcome Guest, Not a member yet? Register   Sign In
Call to a member function query() on a non-object
#1

[eluser]Gork[/eluser]
I'm totally lost and need help. I have a user model to see if a user is in the database. I'll post all the code. Please someone tell me what am I doing wrong. I've been one this for two days now.
This is my Model class, but I'm only working on getUser function.
Code:
<?php
/**
* Created by Eric Evans
* Date: 7/2/12
* Time: 8:46 AM
*/
class User extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /* Utility Methods */
    function _required($required, $data)
    {
        foreach ($required as $field)
            if (!isset($data[$field])) return false;

        return true;
    }

    function _default($defaults, $options)
    {
        return array_merge($defaults, $options);
    }

    /**
     *AddUser method that created a user
     * ---------------------------------
     * Options
     *
     * user_email       required
     * user_password    required
     *
     * @params          options array
     * @return          int insert_id()
     */
    public function addUser($options = array())
    {
        if (!$this->_required(array('user_email', 'user_password'))) {

        }

    }

    /**
     * UpdateUser method to update an existing user
     * ------------------------------------------
     *
     * Options
     *
     * user_id
     * user_email
     * user_password
     *
     * @params          options array
     * @return          affected_rows()
     */
    public function updateUser()
    {

    }

    /**
     * DeleteUser method deletes a user
     * -----------------------------------------
     *
     * Options
     *
     * user_id
     * user_email
     * user_password
     *
     * @param array $options
     * @return int deleted user
     *
     */
    public function deleteUser()
    {

    }

    /**
     * GetUsers method gets a user or group of users
     * -----------------------------------------
     *
     * Options
     *
     * user_id
     * user_email
     * user_password
     * limit                limit to a specified number of records
     *
     * @param array $options
     * @return array objects
     */

    public function getUser($email, $password)
    {
        $sql = 'SELECT * FROM user WHERE user_email=? AND user_password=? LIMIT 1';
        $query = $this->db->query($sql, array($email,$password));

        return ($query->num_rows() > 0) ? $query->row() : false;
    }

    public function GetUserByEmail($email)
    {
        $sql = 'SELECT * FROM user WHERE user_email=? LIMIT 1';
        $query = $this->db->query($sql, array($email));

        return ($query->num_rows() > 0) ? $query->row() : false;
    }
}
Here's my controller that is calling the model to get the user. And when I submit the form I get a Call to a member function query() on a non-object error any help is greatly appreciated.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller
{

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

    public function index()
    {
        $this->load->view('main/index.php');
    }

    public function login()
    {
        $this->form_validation->set_rules('user_email', 'email', 'trim|required|valid_email|callback__check_login');
        $this->form_validation->set_rules('user_password', 'password', 'trim|required');

        if ($this->form_validation->run())
        {
            //Successfully Submitted Login
        }
        $this->load->view('main/login_form');
    }

    public function _check_login()
    {
        if ($_POST)
        {
            $user = $this->User->getUser($_POST['user_email'], md5($_POST['user_password']));
            if ($user)
                return true;
        }
        $this->form_validation->set_message('_check_login', 'Your username/password Combination is invalid.');
        return false;
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
And here's my view.
Code:
<?=form_open('main/login')?>
<fieldset>

    <legend>Login Form</legend>
    <ul>
        <li>
            <label>Email</label>
            &lt;?=form_input('user_email')?&gt;
            &lt;?=form_error('user_email')?&gt;

        </li>
        <li>
            <label>Password</label>
            &lt;?=form_password('user_password')?&gt;
            &lt;?=form_error('user_password')?&gt;

        </li>
        <li>
            &lt;?=form_submit('submit','Submit')?&gt;
        </li>
    </ul>
</fieldset>
&lt;?=form_close()?&gt;
Thanks for the help. Also I have database and user model autoloaded in autoload.php
#2

[eluser]skunkbad[/eluser]
You need to load database
#3

[eluser]Gork[/eluser]
I've already loded the database in the autoad.php file
#4

[eluser]skunkbad[/eluser]
Well, that's what your error is telling you. Maybe you didnt load it right?
#5

[eluser]Gork[/eluser]
I don't know if the windows environment was set l right, I seet my linux environment up and the same code with no modification work no problem. Thanks for the help though
#6

[eluser]cartalot[/eluser]
>And when I submit the form I get a Call to a member function query() on a non-object error any help is greatly appreciated.

well you could have the database loaded -- but you might be doing something like returning an array,
and then trying to process it like its an object.

suggestion -- in your model, use codeigniter active record, and then make a really simple test to try and fetch a record from the database
that will tell you whether the database is working or not -- then you can track down the bug.
#7

[eluser]InsiteFX[/eluser]
Well I do not see in your code were you are loading your model.
#8

[eluser]Gork[/eluser]
Well I have my model autoload in my autoload.php file.




Theme © iAndrew 2016 - Forum software by © MyBB