phpcodeignitersessionquery-builder

Codeigniter. Checking session, num_rows() returning total count


I am experiencing a rather intriguing problem, and I have no idea why.

Utilizing the code below, if a session id is set, the echo statement returns '1' - it finds the user with that session id in the database.. simple.

If however the user has logged out, and 'my_session_id' has been set to 0 such that no result is found in the database, the echo statement seems to be returning the number of rows in the table... I.E as though the only statement was $query=$this->db->get('users');

Any ideas as to why?

$session=$this->session->userdata('my_session_id');
$this->db->where('session',$session);
$query=$this->db->get('users');

$count=$query->num_rows();



//echo $session."<br>";
echo $count;

Solution

  • Probably because the missing session value returns zero which then causes the where part to be ignored. Adding quotes might help:

    $this->db->where('session',"$session");