Php 如何将数据库中的数据从模型发送到控制器codeigneter
模型 //控制器Php 如何将数据库中的数据从模型发送到控制器codeigneter,php,codeigniter,Php,Codeigniter,模型 //控制器 public function sign_in() { if (isset($_POST)) { $this->load->library('session'); $Email = $this->input->post('Email'); $Password = $this->input->post('Password'); $this->db->select
public function sign_in()
{
if (isset($_POST)) {
$this->load->library('session');
$Email = $this->input->post('Email');
$Password = $this->input->post('Password');
$this->db->select('id', 'Name', 'Password', 'Email');
$this->db->from('users');
$this->db->where('Email', $Email);
$this->db->where('Password', md5($Password));
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$data = array();
foreach ($query->result() as $row)
{
$data[] = array(
'Name' => $row->Name
);
}
return $data;
} else {
return false;
}
}
}
//结果
尝试将模型中的if语句更改为:
public function index()
{
$this->load->model('Login');
$data = $this->Login->sign_in();
if ($data) {
$this->load->view('index', $data);
echo 'success';
print_r($data);
} else {
$this->load->view('index');
}
}
问题源于如何将值分配给
$data
数组。我也为您简化了if语句中的逻辑;由于您在活动记录查询中仅通过限制(1)
返回一行,因此不需要使用foreach
尝试下面未测试的代码。同样在控制器上,您使用了$data,我想可能是混淆了,所以将其更改为user_info codeigniter,并且您没有在控制器上为name设置任何变量
模型
控制器
public function sign_in()
{
if (isset($_POST)) {
$this->load->library('session');
$email = $this->input->post('email');
$password = $this->input->post('password');
// Check is the same on table in database case sensitive I think
$this->db->select('id', 'name', 'email');
$this->db->from('users');
$this->db->where('email', $email);
// I would not use MD5 Not Secure Any More
$this->db->where('password', md5($password));
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->row_array();
} else {
return false;
}
}
在望
public function index() {
$this->load->model('Login');
$user_info = $this->Login->sign_in();
if ($user_info) {
$data['id'] = $user_info['id'];
$data['name'] = $user_info['name'];
$data['email'] = $user_info['email'];
$this->load->view('index', $data);
echo 'success';
print_r($user_info);
} else {
$this->load->view('index');
}
}
我也不确定您在提交表单时是否使用了codeigniter的任何表单验证
这里的问题是您的模型,特别是您的查询
选择
设置为检索以下内容:'id',Name',Password',Email'
,但实际上(根据您的代码),您只需要Name
$query->result()
是一个返回对象数组的Codeigniter函数。因此,您不需要遍历它并创建另一个数组。您只需返回这些结果,然后让控制器使用->
操作符进行迭代,以获取对象数据<?php echo $name;?>
我会像这样重写您的代码: 型号:
public function sign_in()
{
if (isset($_POST)) { //POST info should be set in the controller, not in the model
$this->load->library('session'); //Why do you need this??
$Email = $this->input->post('Email'); ///POST info should be set in the controller, not in the model
$Password = $this->input->post('Password');//POST info should be set in the controller, not in the model
$this->db->select('id', 'Name', 'Password', 'Email'); // why do you require all these, if you are only returning the NAME ?
$this->db->from('users');
$this->db->where('Email', $Email);
$this->db->where('Password', md5($Password));
$this->db->limit(1); // why limit, if there should already only be one account that matches?
$query = $this->db->get();
//the code below is iterating for no purpose.
//If the reason why youre doing this iteration is to obtain arrays rather than arrays of objects,
//then use $this->db->result_array() instead
//also, the conditional is not necessary as it will already return false (0) if none found.
if ($query->num_rows() > 0) {
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'Name' => $row->Name
);
}
return $data;
} else {
return false;
}
}
}
public function sign_in($Email, $Password) {
$this->db->select('Name');
$this->db->from('users');
$this->db->where('Email', $Email);
$this->db->where('Password', md5($Password));
$query = $this->db->get();
return $query->row();
}
}
public function index() {
$data = array();
if(isset($_POST)){
$this->load->model('Login');
$Email = $this->input->post('Email');
$Password = $this->input->post('Password');
$result = $this->Login->sign_in($Email, $Password);
if ($result) {
$data["user_info"] = $result;
}
}
$this->load->view('index', $data);
}
控制器:
public function sign_in()
{
if (isset($_POST)) { //POST info should be set in the controller, not in the model
$this->load->library('session'); //Why do you need this??
$Email = $this->input->post('Email'); ///POST info should be set in the controller, not in the model
$Password = $this->input->post('Password');//POST info should be set in the controller, not in the model
$this->db->select('id', 'Name', 'Password', 'Email'); // why do you require all these, if you are only returning the NAME ?
$this->db->from('users');
$this->db->where('Email', $Email);
$this->db->where('Password', md5($Password));
$this->db->limit(1); // why limit, if there should already only be one account that matches?
$query = $this->db->get();
//the code below is iterating for no purpose.
//If the reason why youre doing this iteration is to obtain arrays rather than arrays of objects,
//then use $this->db->result_array() instead
//also, the conditional is not necessary as it will already return false (0) if none found.
if ($query->num_rows() > 0) {
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'Name' => $row->Name
);
}
return $data;
} else {
return false;
}
}
}
public function sign_in($Email, $Password) {
$this->db->select('Name');
$this->db->from('users');
$this->db->where('Email', $Email);
$this->db->where('Password', md5($Password));
$query = $this->db->get();
return $query->row();
}
}
public function index() {
$data = array();
if(isset($_POST)){
$this->load->model('Login');
$Email = $this->input->post('Email');
$Password = $this->input->post('Password');
$result = $this->Login->sign_in($Email, $Password);
if ($result) {
$data["user_info"] = $result;
}
}
$this->load->view('index', $data);
}
查看:
public function sign_in()
{
if (isset($_POST)) { //POST info should be set in the controller, not in the model
$this->load->library('session'); //Why do you need this??
$Email = $this->input->post('Email'); ///POST info should be set in the controller, not in the model
$Password = $this->input->post('Password');//POST info should be set in the controller, not in the model
$this->db->select('id', 'Name', 'Password', 'Email'); // why do you require all these, if you are only returning the NAME ?
$this->db->from('users');
$this->db->where('Email', $Email);
$this->db->where('Password', md5($Password));
$this->db->limit(1); // why limit, if there should already only be one account that matches?
$query = $this->db->get();
//the code below is iterating for no purpose.
//If the reason why youre doing this iteration is to obtain arrays rather than arrays of objects,
//then use $this->db->result_array() instead
//also, the conditional is not necessary as it will already return false (0) if none found.
if ($query->num_rows() > 0) {
$data = array();
foreach ($query->result() as $row) {
$data[] = array(
'Name' => $row->Name
);
}
return $data;
} else {
return false;
}
}
}
public function sign_in($Email, $Password) {
$this->db->select('Name');
$this->db->from('users');
$this->db->where('Email', $Email);
$this->db->where('Password', md5($Password));
$query = $this->db->get();
return $query->row();
}
}
public function index() {
$data = array();
if(isset($_POST)){
$this->load->model('Login');
$Email = $this->input->post('Email');
$Password = $this->input->post('Password');
$result = $this->Login->sign_in($Email, $Password);
if ($result) {
$data["user_info"] = $result;
}
}
$this->load->view('index', $data);
}
尝试将此
$query->result()
更改为$query->result\u array()
变量转储($row)
,然后查看它是真实的properties@wolfgang1983抱歉,忘记了结果
上的括号。原帖已编辑。