Database acting crazy

#1
[eluser]Unknown[/eluser]
Hello chaps,

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

I have the following model:

Code:
function GetRestaurants($options = array())
    {
        // Qualification
        if(isset($options['restaurantID']))
            $this->db->where('restaurantID', $options['restaurantID']);
        if(isset($options['restaurantRegionID']))
            $this->db->where('restaurantRegionID', $options['restaurantRegionID']);
        if(isset($options['restaurantName']))
            $this->db->where('restaurantName', $options['restaurantName']);
        if(isset($options['restaurantAddress1']))
            $this->db->where('restaurantAddress1', $options['restaurantAddress1']);
        if(isset($options['restaurantAddress2']))
            $this->db->where('restaurantAddress2', $options['restaurantAddress2']);
        if(isset($options['restaurantSuburb']))
            $this->db->where('restaurantSuburb', $options['restaurantSuburb']);
        if(isset($options['restaurantCity']))
            $this->db->where('restaurantCity', $options['restaurantCity']);
        if(isset($options['restaurantInformation']))
            $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']))
            $this->db->limit($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();
        
        if(isset($options['restaurantID']))
            return $query->row(0);
            
        if(isset($options['limit']) && $options['limit'] == '1')
            return $query->row(0);
            
        return $query->result();
    }

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

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

Even though the result of
Code:
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!

#2
[eluser]tonanbarbarian[/eluser]
are you sure there is not a space at the start or the end of the 2nd segment?
try
Code:
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
Code:
$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.

#3
[eluser]Unknown[/eluser]
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.