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