Php 致命错误:对中的非对象调用成员函数where()
我一直在调试这段代码,但仍然没有成功。有人能帮我吗Php 致命错误:对中的非对象调用成员函数where(),php,codeigniter,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
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;
}
}
我不断地在电话里听到一个致命的错误
$this->db->where('username'),$this->input->post('username')
这是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();
}
是否
load->model('membership\u model')
应该接收类的名称,并且该名称区分大小写?您可能应该检查API函数的返回值…在调用validate之前,数据库可能没有正确初始化。看起来您没有连接到数据库。确保您正在连接到数据库
您可以在每次脚本运行时自动连接,也可以手动连接到数据库。查看CI指南了解您的连接选项:首先,您需要在构造函数中声明
$this->db
,您可以给我一个声明的示例吗?我认为您可以在加载模型时通过将TRUE传递给第三个参数来自动连接到数据库,如$this->load->model('Model_name','',TRUE);