2015-10-16 63 views
0

我在CI模式简单的代码登录,但它始终返回0NUM_ROWS返回零所有的时间笨

 $this->db->select('*'); 
    $this->db->from('cms_users'); 
    $this->db->where('username', "admin"); 
    $this->db->where('password', "admin01"); 
    $query = $this->db->get(); 
    echo $query->num_rows();exit; 

它总是打印0

当我尝试echo $this->db->get_compiled_select();exit;并运行phpmyadmin它返回一个值。

+0

你在你的数据库检查你确实有一个用户= '管理员',密码= 'admin01'在你的表cms_users中? – Bak

+0

其实你不需要$ this-> db-> select('*');但是使用$ this-> db-> get()默认是这样做的 – Bak

回答

1

我想是因为你呼应,而不是回报

而且你必须退出在那里还有我不会用退出;

型号:User_model.php

<?php 

class User_model extends CI_Model { 

    public function count() { 
     $this->db->where('username', "admin"); 
     $this->db->where('password', "admin01"); 
     $query = $this->db->get($this->db->dbprefix . 'cms_users'); 
     return $query->num_rows(); 
    } 

} 

控制器:Weclome.php

<?php 

class Welcome extends CI_Controller { 

    public function __construct() { 
     parent::__construct(); 
     $this->load->model('user_model'); 
    } 

    public function index() { 
     $data['user_total'] = $this->user_model->count(); 

     $this->load->view('welcome_message', $data); 
    } 

}