致命错误:调用成员函数where()在非对象中

时间:2011-05-11 05:45:56

标签: php codeigniter

我一直在调试这段代码,但仍然没有成功。有人可以帮帮我吗?

class Membership_model extends CI_Model {

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

function validate()
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', md5($this->input->post('password')));
    $query = $this->db->get('membership');

    if($query->num_rows == 1)
    {
        return true;
    }
}

function create_member()
{

    $new_member_insert_data = array(
        'first_name' => $this->input->post('first_name'),
        'last_name' => $this->input->post('last_name'),
        'email_address' => $this->input->post('email_address'),         
        'username' => $this->input->post('username'),
        'password' => md5($this->input->post('password'))                       
    );

    $insert = $this->db->insert('membership', $new_member_insert_data);
    return $insert;
}
}

我继续在线上发生致命错误

  

$这 - > DB-化合物其中( '用户名',$这 - >输入 - >柱( '用户名'));

这是controller / login.php

class Login extends CI_Controller {

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

function index()
{
    $this->load->helper('url');
    $data['main_content'] = 'login_form';
    $this->load->view('includes/template', $data);      
}

function validate_credentials()
{       
    $this->load->model('membership_model');
    $query = $this->membership_model->validate();

    if($query) // if the user's credentials validated...
    {
        $data = array(
            'username' => $this->input->post('username'),
            'is_logged_in' => true
        );
        $this->session->set_userdata($data);
        redirect('site/members_area');
    }
    else // incorrect username or password
    {
        $this->index();
    }
}   

function signup()
{
    $data['main_content'] = 'signup_form';
    $this->load->view('includes/template', $data);
}

function create_member()
{
    $this->load->library('form_validation');

    // field name, error message, validation rules
    $this->form_validation->set_rules('first_name', 'Name', 'trim|required');
    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
    $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
    $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');


    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('signup_form');
    }

    else
    {           
        $this->load->model('membership_model');

        if($query = $this->membership_model->create_member())
        {
            $data['main_content'] = 'signup_successful';
            $this->load->view('includes/template', $data);
        }
        else
        {
            $this->load->view('signup_form');           
        }
    }

}

function logout()
{
    $this->session->sess_destroy();
    $this->index();
}

3 个答案:

答案 0 :(得分:0)

可能是load->model( 'membership_model' )应该收到一个类的名称,并且该名称是区分大小写吗?您应该检查API函数的返回值...

答案 1 :(得分:0)

在调用validate之前,数据库可能未正确初始化。

答案 2 :(得分:0)

看起来您没有连接到您的数据库。确保连接到数据库。

您可以在每次脚本运行时自动连接或手动连接到数据库。查看CI指南,了解您的连接选项:http://codeigniter.com/user_guide/database/connecting.html