CodeIgniter 3查询ESCAPE'!'忽略WHERE语句?

时间:2015-10-22 15:19:49

标签: php mysql codeigniter escaping sql-like

我正在使用选择框构建网站搜索以获取某些类别&区域。 如果选中,我将它包含在查询中,但出于某种原因,使用LIKE是忽略查询的WHERE部分!?!?为什么这样或我做错了什么?

当我回应Codeigniter构建的查询时,我得到了fololowing: (注意 ESCAPE'!'

查询回音:

      SELECT SQL_CALC_FOUND_ROWS null as rows, ads.id AS id, location, provLabel, text, 
  adcat.id AS catid, ads.subcatid AS subcatid, ads.province R_rand, r_option, addate, 
  adcat.name AS catname, adsubcat.name AS subname, f_value, adtitle, ads.area, regionLabel, 
  adlink 
  FROM `ads` 
  JOIN `search_town` ON `search_town`.`townId`=`ads`.`townId` 
  JOIN `search_region` ON `search_region`.`regionId`=`ads`.`area` 
  JOIN `search_prov` ON `search_prov`.`provId`=`ads`.`province` 
  JOIN `adcat` ON `adcat`.`id`=`ads`.`catid` 
  JOIN `adsubcat` ON `adsubcat`.`id`=`ads`.`subcatid` 
  LEFT JOIN `adfields` ON `adfields`.`ad_id`=`ads`.`id` 
  WHERE `ads`.`catid` != 8 AND `ads`.`adactive` = 1 AND `scam` =0 AND `ads`.`province` = '1' 
  AND `ads`.`catid` = '3' AND `text` LIKE '%Nissan%' ESCAPE '!' 
  OR `f_value` LIKE '%Nissan%' ESCAPE '!' 
  GROUP BY `ads`.`id` 
  ORDER BY `addate` DESC 
  LIMIT 10

以下是控制器中的实际查询:

        public function get_search($fsearch, $fcategory, $fprovince, $farea, $limit, $start)
    { 
      if($fcategory >=1){
      $incl_cat=" AND ads.catid='$fcategory'"; 
      }else{
      $incl_cat='';
      }
      if($fprovince>=1){
      $incl_prov=" AND ads.province='$fprovince'";
      }else{
      $incl_prov='';
      }
      if($farea >= 1){
      $incl_area=" AND ads.area='$farea'";
      }else{
      $incl_area='';
      }                            

       $this->db->select('SQL_CALC_FOUND_ROWS null as rows, ads.id AS id, location, provLabel, text, adcat.id AS catid, ads.subcatid AS subcatid,ads.province  
      R_rand, r_option, addate, adcat.name AS catname, adsubcat.name AS subname, f_value, adtitle, ads.area, regionLabel,
      adlink', FALSE);
       $this->db->from('ads');
       $this->db->join('search_town', 'search_town.townId=ads.townId');
       $this->db->join('search_region', 'search_region.regionId=ads.area');
       $this->db->join('search_prov', 'search_prov.provId=ads.province');
       $this->db->join('adcat', 'adcat.id=ads.catid');
       $this->db->join('adsubcat', 'adsubcat.id=ads.subcatid');
       $this->db->join('adfields', 'adfields.ad_id=ads.id', 'left');
       $where = "ads.catid!=8 AND ads.adactive=1 AND scam=0 $incl_prov $incl_cat $incl_area";
       $this->db->where($where);
       $this->db->like('text', $fsearch);
       $this->db->or_like('f_value', $fsearch);
       $this->db->group_by("ads.id");
       $this->db->order_by('addate', 'DESC');
       $this->db->limit($limit, $start);
       $query = $this->db->get();
       $return = $query->result_array();
       echo $this->db->last_query();
       $total_results=$this->db->query('SELECT FOUND_ROWS() count;')->row()->count;
       $this->session->set_userdata('tot_search', $total_results);
       return $return;

    }

1 个答案:

答案 0 :(得分:2)

你的WHERE条件是

a AND b AND c AND d AND ... AND x OR y

如果y为真,则整个WHERE条件为真。

也许你的意思是:

a AND b AND c AND d AND ... AND (x OR y)

ESCAPE字符默认为\,但看起来codeigniter已将此更改为!(大概是因为\已经是PHP的转义,所以有时你需要倍数这可能令人困惑)。

目前这与您的查询无关。只有当您需要将%_!%!_(默认\%\_)匹配时,才会使用此选项。