Welcome Guest, Not a member yet? Register   Sign In
num_rows() problem
#11

[eluser]Cristian Gilè[/eluser]
[quote author="InsiteFX" date="1296127109"]It returned 1 for the row because it did find the row in the database.

InsiteFX[/quote]

It's not right! A session var that is not set or is set as empty like this:

Code:
$this->session->set_userdata('username',"");

doesn't exist, so the statement
Code:
if($this->session->userdata('username') == "")

returns TRUE.


Cristian Gilè
#12

[eluser]eldrinofsoalim[/eluser]
Thanks for the insight Cristian,

However I am still wondering why the Active Record code:
Quote:$query1 = $this->db->get_where(‘users’, array(‘username’ => $this->session->userdata(‘username’)));
print_r ($query1->num_rows());

...is still returning num_rows of "1" when obviously I do not have a username called "" (empty string) in the table 'users' (my session cookie var 'username' is not yet set, hence it is empty)?
#13

[eluser]Cristian Gilè[/eluser]
Code:
$this->session->userdata(‘username’)
is not set so userdata returns FALSE (0). The resulting query is:

Code:
SELECT * FROM users WHERE username = 0

The = operator cannot compare two values of different types. Mysql does an implicit cast of 'username' to a numeric type. 'username' cannot be parsed into a number, and gets converted to the nearest thing, which happens to be 0.
'username' = 0 becomes 0 = 0, which is true, so the row is fetched and num_rows() returns 1.



Cristian Gilè




Theme © iAndrew 2016 - Forum software by © MyBB