2014-04-04 57 views
0

我真的无法看到问题出在哪里。我的查询没有返回正确的计数

EX:宝马是在纽约(city_id = 5)和康涅狄格销售(city_id = 3),因此,当用户在搜索框中返回2分的结果,它应该只是进入BMW 1

public function search_result_count($search) { 
    $count=0; 

    $search = addslashes($search); 

    $city_id = $this->tank_auth->get_user_city()->id; 

    $sql="select count(id) as count from ".$this->table_name." 

      where title LIKE '%$search%' or zipcode like '%$search%' 

      and city_id ='$city_id' "; 

    $query=$this->db->query($sql); 

    if($row=$query->row()) { 

    $count=$row->count; 

    } 

    return $count; 
} 

即使我分配$ city_id =“5”仍然是同样的事情,这里会出现什么问题?

回答

0
select count(id) as count from ".$this->table_name." 

       where (title LIKE '%$search%' or zipcode like '%$search%') 

       and (city_id ='$city_id) 
1

尝试用GROUP BY你错过集团宝马字段名

$sql="select count(id) as count from ".$this->table_name." 

     where ((title LIKE '%$search%' or zipcode like '%$search%') 

     and city_id ='$city_id') GROUP BY $BMW_COL "; 

1

(city_id ='$city_id') condtion将在OR声明的第一部分被跳过。尝试这样

$sql="select count(id) as count from ".$this->table_name." 

     where ((title LIKE '%$search%') and (city_id ='$city_id')) 

             or 

       ((zipcode like '%$search%') and (city_id ='$city_id')) "; 
1

尝试在CI格式

$this->db->select('count(id) as count'); 
    $this->db->from('$this->table_name'); 
    $this->db->where('city_id', $city_id); 
    $this->db->where("title LIKE '%".$search."%' or zipcode like '%".$search."%'"); 
    $query = $this->db->get(); 
    if($query->num_rows() > 0){ 
      return $query->result_array(); 
     } 
    }