Weird issue: alphabetical values ignored in queries

I'm tremendously baffled and annoyed by this.

I'm passing a third URL segment that is a user id.


The 3rd segment is captured by my function:

function user($user_id)
        if ($user_id && valid_user($user_id)) //if segment passed and user exists
            //get user's profile data
            $data['row'] = $this->Profile_model->profile_read($user_id);            
            $this->load->view('profile/public/user_view', $data);


So the function above returns data for user 63. If I type a user id that does not exists, e.g. 6347267, it redirects home. However if I add letters to a valid user id, e.g. 63ghjfs it somehow gets the right data. It seems to somehow ignore the letters.

The query looks like this:

SELECT * FROM (`users`) WHERE `user_id` = '62ghjfs' LIMIT 1

User ids are strictly integers. How in the world is it returning the right data when id "62ghjfs" does not exist! This is so confusing..

[eluser]Eric Barnes[/eluser]
My first advice is to cast $user_id to an int so that you are 100% positive you are in fact with the expected data type.
$user_id = (int) $user_id;

Thanks for the advice Eric! I will definitely do that.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.