Codeigniter数据库问题

时间:2010-10-15 00:30:59

标签: mysql database codeigniter

试图从我的数据库中获取一些数据。

我有以下型号:

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();
    } 

现在,以下代码可以正常运行:

$this->load->model('Restaurants_model');
        $data['restaurant'] =  $this->Restaurants_model->GetRestaurants(array(
            'restaurantName' => 'shed 5', 
            'limit' => '1'
            )); 

但以下情况不起作用:

$this->load->model('Restaurants_model');
        $data['restaurant'] =  $this->Restaurants_model->GetRestaurants(array(
            'restaurantName' => str_replace('-', ' ', $this->uri->segment(2)), 
            'limit' => '1'
            )); 

即使是

的结果
str_replace('-', ' ', $this->uri->segment(2)) 

在这种情况下:'脱落5'。

我比较了str_replace输出的var_dumps和字符串本身,并确定它们是相同的。那么为什么直接字符串返回结果但是从uri段生成的字符串不是?某种编码问题?我的数据库保存'utf8_general_ci'中的数据。

感谢您的任何建议!

1 个答案:

答案 0 :(得分:0)

$ restaurant = str_replace(' - ','',$ this-> uri-> segment(2));

获取数组外的值并尝试使用array_push

$ data ['restaurant'] = $ this-> Restaurants_model-> GetRestaurants(array(             'restaurantName'=> $餐厅,             'limit'=> '1'             ));