0

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;
3
  • you can check last query ran by using echo $this->db->last_query();. Can you check if appropriate query is formed Commented Dec 18, 2011 at 0:16
  • in all cases the correct query is being displayed.. strange Commented Dec 18, 2011 at 0:38
  • What does it mean to say that the "correct query is being displayed strange"? Is the rendered SQL showing WHERE `session` IS NULL? If you want to return the number of rows, then see count_all_results(). Commented Nov 1 at 14:17

2 Answers 2

0

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");
Sign up to request clarification or add additional context in comments.

3 Comments

What in the world? Why does this matte? I don't understand how double quoting the variable will make any difference.
Oh man, it's been 14 years... I assume that the userdata method returns NULL if the item is not found, and that invalidates the WHERE statement completely, as opposed to "NULL" which causes the WHERE statement to fire, yet find nothing
Okay. I don't know how earlier versions of CI's where() treated a null value as the second parameter. I was assuming that IS NULL would be rendered.
0

Is the session column of users table character type or numeric type? If it is integer, your code should work; however if it is a character type, your code is throwing following SQL:

SELECT * FROM (`users`) WHERE `session` = 0

which will return all records in users table. It has to be:

SELECT * FROM (`users`) WHERE `session` = '0'

In order to do this, your code needs to be something like:

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

Hope this help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.