Home >Backend Development >PHP Tutorial >ci框架$this->db->where得不到结果

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

WBOY
WBOYOriginal
2016-06-06 20:14:161299browse

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

<code>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();
        }
</code>

以下是模型中的代码:

<code>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;
        }</code>

回复内容:

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

<code>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();
        }
</code>

以下是模型中的代码:

<code>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;
        }</code>

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

<code>public function login($user, $password)
    {
        return $this->db->where('username', $user)
            ->where('password', sha1($password))
            ->get('user')
            ->row();
    }
</code>
<code>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;</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn