CodeIgniter中带有Active Record的“count_all_results”和“where”的问题

时间:2011-09-26 10:03:33

标签: php database codeigniter

在CodeIngiter用户指南中,他们说了以下代码:

$this->db->where('name', $name);
$this->db->where('title', $title);
$this->db->where('status', $status); 
// WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

这意味着当我想通过活动记录从数据库中选择一些东西时,我应该使用where方法,它将通过在字段之间替换AND来实现。 现在我想创建登录页面,我这样做:

public function True_login($username = '',$password = '')
    {
        $this->db->flush_cache();
        $this->db->where('username',$username);
        $this->db->where('password',$password);
        $count = $this->db->count_all_results('PM_ADMIN_LIST');
        if ($count === 1)
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
但如果username=$username OR password = $password,它将返回TRUE。 如果在表格中找到其中一个用户名或密码($count === 1将返回TRUE) 我的prbolem在哪里?我应该如何解决它?

3 个答案:

答案 0 :(得分:12)

$this->db->count_all_results('PM_ADMIN_LIST');

返回表中的行数并忽略它上面的限制器。

尝试: -

$this->db->where('username',$username);
$this->db->where('password',$password);
$this->db->from('PM_ADMIN_LIST');
$count = $this->db->count_all_results();

此外,进行一些调试 - 如果您知道$count的价值是什么,那么它可能帮助您确定Active Record查询是错误的而不是认为它正在执行OR而不是AND

答案 1 :(得分:2)

只需使用 - > $这 - > DB-> count_all(); 代替 $这 - > DB-> count_all_results(); 问题解决了.......

答案 2 :(得分:-1)

count_all_results()函数中添加值为false的第二个参数。

$this->db->count_all_results('PM_ADMIN_LIST',FALSE);