Moein Hosseini
Moein Hosseini

Reputation: 4383

problem with "count_all_results" and "where" with Active Record in CodeIgniter

In CodeIngiter User guide ,they said the following code:

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

It means when I wanna select some thing from database by active record,I should use where method and it will do it by replace AND between fields. now I wanna to make login page,I do this:

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;
        }
    }
but it will return TRUE if username=$username OR password = $password . if one of the username or password will be found in table(and $count === 1 it will return TRUE) where is my prbolem and how should I solve it?

Upvotes: 5

Views: 18249

Answers (3)

Avnish Tiwary
Avnish Tiwary

Reputation: 2276

Add second parameter with value false in count_all_results() function.

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

Upvotes: -1

Sachin Shukla
Sachin Shukla

Reputation: 1279

Simply use-> $this->db->count_all(); instead of $this->db->count_all_results(); problem resolved.......

Upvotes: 2

Bruce
Bruce

Reputation: 1542

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

is returning the number of rows in the table and ignorning the restrictors above it.

Try:-

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

Also, put some debug in - if you knew what the value of $count was then it may have helped you work out that the Active Record query was wrong rather than thinking it was doing an OR instead of an AND.

Upvotes: 12

Related Questions