CodeIgniter:如何在valitdaion之后返回表格行中的所有数据?

时间:2015-12-21 11:56:21

标签: php mysql codeigniter

我在模型文件夹中有这个公共函数,它从控制器获取用户名和密码的输入:

// Validate that the user is registered and returns true or false. 
public function validate($email, $password) {

    $this->db->select('u_email', 'u_password');
    $this->db->from('users');
    $this->db->where('u_email', $email);
    $this->db->where('u_password', $password);

    $query = $this->db->get();

    if ( $query->num_rows() == 1 ) {
        return $query->result();
    } else {
        return FALSE;
    }


}

我希望它返回属于此用户名+密码的所有行信息(有一个u_id,u_email,u_name,u_permissions)。

所以我可以在控制器中使用并保存在会话中:

    $this->load->model('membership_model'); 
    $query = $this->membership_model->validate($email, $password);


    if ( $query ) {  // if the user's credentials validated...

        $data = array(
                'u_id'  => $query->u_id,
                'email' => $query->u_email, 
                'name'  => $query->u_name,
                'permittions' => $query->u_permittion,
                'is_logged_in' => TRUE
            );

        $this->session->set_userdata($data);

1 个答案:

答案 0 :(得分:2)

只需使用*获取所有数据,然后使用row()获取单个获取行

$this->db->select('*');// use * here
$this->db->from('users');
$this->db->where('u_email', $email);
$this->db->where('u_password', $password);

$query = $this->db->get();

if ( $query->num_rows() == 1 ) {
    return $query->row();// use row();
} else {
    return FALSE;
}

阅读https://www.codeigniter.com/user_guide/database/examples.html