Database acting crazy

Hello chaps,

Having a spot of bother trying to grab some data out of my database.

I have the following model:

function GetRestaurants($options = array())
        // Qualification
            $this->db->where('restaurantID', $options['restaurantID']);
            $this->db->where('restaurantRegionID', $options['restaurantRegionID']);
            $this->db->where('restaurantName', $options['restaurantName']);
            $this->db->where('restaurantAddress1', $options['restaurantAddress1']);
            $this->db->where('restaurantAddress2', $options['restaurantAddress2']);
            $this->db->where('restaurantSuburb', $options['restaurantSuburb']);
            $this->db->where('restaurantCity', $options['restaurantCity']);
            $this->db->where('restaurantInformation', $options['restaurantInformation']);
        // limit / offset
        if(isset($options['limit']) && isset($options['offset']))
            $this->db->limit($options['limit'], $options['offset']);
        else if(isset($options['limit']))
        // sort
        if(isset($options['sortBy']) && isset($options['sortDirection']))
            $this->db->order_by($options['sortBy'], $options['sortDirection']);
        $query = $this->db->get("tblRestaurants");
        if(isset($options['count'])) return $query->num_rows();
            return $query->row(0);
        if(isset($options['limit']) && $options['limit'] == '1')
            return $query->row(0);
        return $query->result();

Now the following code works fine:
        $data['restaurant'] =  $this->Restaurants_model->GetRestaurants(array(
            'restaurantName' => 'shed 5',
            'limit' => '1'

However the following does not work:
        $data['restaurant'] =  $this->Restaurants_model->GetRestaurants(array(
            'restaurantName' => str_replace('-', ' ', $this->uri->segment(2)),
            'limit' => '1'

Even though the result of
str_replace('-', ' ', $this->uri->segment(2))
is in this instance: 'shed 5'.

I have compared var_dumps of the output of the str_replace and the string itself and determined them to be identical. So why does the straight string return a result yet the string generated from the uri segment doesn't? Some kind of encoding issue? My database holds data in 'utf8_general_ci'.

Thanks for any suggestions!

are you sure there is not a space at the start or the end of the 2nd segment?
echo '#'.str_replace('-', ' ', $this->uri->segment(2)).'#';
if it shows #shed 5 # or # shed 5# then there are extra spaces that need to be taken into account

also you need to check exactly what query the code is producing so make this change
$query = $this->db->get("tblRestaurants");
echo $this->db->last_query();

You should then be able to see the exact query that was run and that might shed light on the issue.

Thanks Tonon I checked the last query and it showed my query was actually using the string 'shed_5' Changed the str_replace from '-' to '_' and it's all working now. I had forgotten that the uri comes in with underscores as I had been using a hook to make dashes work as well.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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