【问题标题】:CodeIgniter: How to return all data in the tables row after valitdaion?CodeIgniter:如何在验证后返回表格行中的所有数据?
【发布时间】:2015-12-21 11:56:21
【问题描述】:

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

// 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);

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    只需使用* 获取所有数据并使用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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      • 2020-11-03
      • 2022-08-05
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      相关资源
      最近更新 更多