Welcome Guest, Not a member yet? Register   Sign In
$this->db->count_all_results() returning 1 instead of 0
#1

[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?
#2

[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.

#3

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

[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.




Theme © iAndrew 2016 - Forum software by © MyBB