作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
模型
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;
}
}
}
// Controller
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');
}
}
//结果
最佳答案
这里的问题是您的模型,特别是您的查询。
您的 SELECT
设置为检索以下内容:'id'、'Name'、'Password'、'Email'
但实际上(根据您的代码),您只需要名称
。
您正在创建一个不必要的数组。您可能知道也可能不知道,$query->result()
是一个返回对象数组的 Codeigniter 函数。因此,您不需要遍历它并创建另一个数组。您需要做的就是返回这些结果,并让您的 Controller 使用 ->
运算符进行迭代以获取您的对象数据。
综上所述,这些是我将在您当前的模型方法中处理的错误。我用注释来解释:
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();
}
}
Controller :
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);
}
View :
print_r($user_info);
//or
echo $user_info->Name;
关于php - 如何将数据从数据库从模型发送到 Controller codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31231633/
我是一名优秀的程序员,十分优秀!