当前位置:Gxlcms > PHP教程 > ci框架$this->db->where得不到结果

ci框架$this->db->where得不到结果

时间:2021-07-01 10:21:17 帮助过:8人阅读

用ci写登录代码,用$this->db->where查询数据库验证账号密码时得到的总是空的返回值
以下是控制器中验证的函数代码:

public function validate_credentials(){
        $this->load->model('Bookmark_models');
        $query = $this->Bookmark_models->where();
        
        if ($query){
            $data = array('username' => $this->input->post('username'),
                          'is_loggrd_in' => true);
            $this->session->set_userdata($data);
            $this->load->view('logged_in_area');
        }else {
            $this->login();
        }

以下是模型中的代码:

public function where(){
        $this->db->where('username',$this->input->post('username'));
        $this->db->where('passwd',sha1($this->input->post('password')));
        $query = $this->db->get('user');
        
        if ($query->num_rows == 1){
            return true;
        }

回复内容:

用ci写登录代码,用$this->db->where查询数据库验证账号密码时得到的总是空的返回值
以下是控制器中验证的函数代码:

public function validate_credentials(){
        $this->load->model('Bookmark_models');
        $query = $this->Bookmark_models->where();
        
        if ($query){
            $data = array('username' => $this->input->post('username'),
                          'is_loggrd_in' => true);
            $this->session->set_userdata($data);
            $this->load->view('logged_in_area');
        }else {
            $this->login();
        }

以下是模型中的代码:

public function where(){
        $this->db->where('username',$this->input->post('username'));
        $this->db->where('passwd',sha1($this->input->post('password')));
        $query = $this->db->get('user');
        
        if ($query->num_rows == 1){
            return true;
        }

echo $this->db->last_query();输出原生sql语句到数据库中执行看有没有结果.
num_rows()是个方法而不是属性
另外也不建议你这么写,模型中就不建议做控制器的事了

public function login($user, $password)
    {
        return $this->db->where('username', $user)
            ->where('password', sha1($password))
            ->get('user')
            ->row();
    }
public function where(){
        $this->db->where('username',$this->input->post('username'));
        $this->db->where('passwd',sha1($this->input->post('password')));
        $query = $this->db->get('user');
        echo $this->db->last_query();exit();
        if ($query->num_rows() ){
            return true;
        }
        return false;

人气教程排行