Php 登录后遇到错误:致命错误:对CodeIgniter中的非对象调用成员函数result()
我在登录后获取数据时遇到问题。请看一下我的代码。我是CodeIgniter的新手,所以请帮助我 我得到这个错误: 致命错误:对中的非对象调用成员函数result() 共点火器 这是我的控制器:Php 登录后遇到错误:致命错误:对CodeIgniter中的非对象调用成员函数result(),php,codeigniter,Php,Codeigniter,我在登录后获取数据时遇到问题。请看一下我的代码。我是CodeIgniter的新手,所以请帮助我 我得到这个错误: 致命错误:对中的非对象调用成员函数result() 共点火器 这是我的控制器: public function login_function() { $this->load->database(); // Load the model $this->load->model('login_model'); //
public function login_function()
{
$this->load->database();
// Load the model
$this->load->model('login_model');
// Load the method of model
$records = $this->login_model->can_login();
// Return the data in view
if ($records === false)
{
$this->session->set_flashdata("message", "invalid user");
redirect('Main_controller', 'refresh');
} else {
$this->session->set_userdata('id', $records['id']);
$data['f'] = $records;
$this->load->view('user-home', $data);
}
}
<?php foreach ($f->result() as $row) { ?>
<h1><?php echo $row->first_name; ?></h1><br /><br />
<?php } ?>
这是我的型号:
function can_login()
{
$email=$this->input->post('email');
$password=$this->input->post('password');
// Data is retrieve from this query
$this->db->select('*');
$this->db->where('email', $email);
$this->db->where('password', $password);
$query1 = $this->db->get('registration');
$result = $query1->result();
if (!empty($result)){
return $query1->row_array();
} else {
return false;
}
}
这是我的观点:
public function login_function()
{
$this->load->database();
// Load the model
$this->load->model('login_model');
// Load the method of model
$records = $this->login_model->can_login();
// Return the data in view
if ($records === false)
{
$this->session->set_flashdata("message", "invalid user");
redirect('Main_controller', 'refresh');
} else {
$this->session->set_userdata('id', $records['id']);
$data['f'] = $records;
$this->load->view('user-home', $data);
}
}
<?php foreach ($f->result() as $row) { ?>
<h1><?php echo $row->first_name; ?></h1><br /><br />
<?php } ?>
您的错误来自您的视图。在$f
上调用result()
方法,这是can\u login()函数的结果
$f
取两个值(根据您的型号):
public function login_function()
{
$this->load->database();
// Load the model
$this->load->model('login_model');
// Load the method of model
$records = $this->login_model->can_login();
// Return the data in view
if ($records === false)
{
$this->session->set_flashdata("message", "invalid user");
redirect('Main_controller', 'refresh');
} else {
$this->session->set_userdata('id', $records['id']);
$data['f'] = $records;
$this->load->view('user-home', $data);
}
}
<?php foreach ($f->result() as $row) { ?>
<h1><?php echo $row->first_name; ?></h1><br /><br />
<?php } ?>
false
如果没有结果李>
- 如果找到结果,则返回一个数组(
row\u array()
)
问题是在行数组()
方法上调用result()
方法(类似于$query1->row\u数组()->result()
),这是错误的
解决方案:您应该在模型中返回$result
变量。然后,在您的视图中,您可以使用以下代码显示结果:
foreach ($f as $row) // $f = $query->result() as you saved it in your model
{
echo $row->first_name;
}
在您的模型中不需要使用下面的代码
if(!empty($result)){
return $query1->row_array();
} else{
return false;
}
只需尝试return$query->result\u数组
或返回$query->result代码>
如果没有结果,它将返回false 是否加载了数据库类,如中所示?在$query1=$this->db->get('registration')之前;不要重复“行”$查询1->num_rows();并报告您得到的是的,我做了。但它仍然不起作用。alex:遇到PHP错误严重性:注意消息:未定义变量:query1文件名:models/login_model.phpit在我放入echo“rows”时正在打印“rows1”$查询1->num_rows();在$query1=$this->db->get('registration')之后;