CodeIgniter Forums
$this->db->count_all_results() returning 1 instead of 0 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: $this->db->count_all_results() returning 1 instead of 0 (/showthread.php?tid=53573)



$this->db->count_all_results() returning 1 instead of 0 - El Forum - 07-30-2012

[eluser]thisischris[/eluser]
I have the following code in a library:



Code:
private $CI;

    public function __construct()
    {
        $this->CI =& get_instance();
    }

    public function authenticate($username, $password)
    {
$query = $this->CI->db->get_where('users', ['name' => $username, 'password' => md5($password)], 1, 0);
        $rowCount = $this->CI->db->count_all_results();
        
        if ($rowCount == 1)
        {
            var_dump($rowCount);
            $record = $query->row();
            $user = ['name' => $record->name, 'id' => $record->id, 'admin' => $record->admin];
            return $user;
        }
        else
        {
            return false;
        }
}

The database is completely empty yet rowCount is being set as 1?


$this->db->count_all_results() returning 1 instead of 0 - El Forum - 07-30-2012

[eluser]tpetrone[/eluser]

Start with simply:

Code:
echo $this->CI->db->count_all_results();

that should display the count..


Perhaps modify the if statement to:
Code:
if($rowCount >= 1)

or

if($rowCount > 0)

That might get you down the road.




$this->db->count_all_results() returning 1 instead of 0 - El Forum - 07-30-2012

[eluser]NeoArc[/eluser]
Don't use get_where (LIMIT 1, OFFSET 0) and count_all_results() at the same time.
Use ->where() instead Smile


$this->db->count_all_results() returning 1 instead of 0 - El Forum - 07-30-2012

[eluser]Aken[/eluser]
Don't use count_all_results() in the first place, because if you want to do that AND return a row, you'll need to run two queries.

Use $query = get_where(...), and then $query->num_rows() to check the rows returned.