【问题标题】:CodeIgniter 3 Query ESCAPE '!' Ignoring the WHERE statement?CodeIgniter 3 查询 ESCAPE '!'忽略 WHERE 语句?
【发布时间】:2016-01-21 22:39:55
【问题描述】:

我正在构建一个带有选择框的站点搜索,以获取某些类别和地区。 如果选择我是否将其包含在查询中,但由于某种原因,使用 LIKE 是否会忽略查询的 WHERE 部分!?!?为什么会这样或者我做错了什么?

当我回显 Codeigniter 构建的查询时,我得到以下信息: (注意 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;

    }

【问题讨论】:

  • ESCAPE '!' 不会影响您的查询,是完全可以接受的 SQL 语法。这意味着,如果您想搜索通配符的文字实例,请在其前面加上“!”。它不会影响您的结果。
  • 感谢@DFriend。非常感谢。
  • 我认为您的查询没有返回结果?
  • 事实上确实如此。下面的 Arth 准确地解释了我遇到的问题。

标签: php mysql codeigniter escaping sql-like


【解决方案1】:

你的 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 中已经是一个转义字符,所以有时您需要多个字符,这可能会造成混淆) .

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

【讨论】:

  • 啊!感谢您指出@Arth 有时会觉得自己像个白痴,错过了显而易见的事情!
  • 再问一个问题@Arth 确保 $this->db->like('text', $fsearch); $this->db->or_like('f_value', $fsearch);是(x 或 y)格式吗?
  • 我不知道代码点火器,但我想它在某个地方的文档中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-26
  • 2015-01-31
  • 2012-09-18
  • 2012-12-29
  • 1970-01-01
  • 2015-02-24
  • 2015-08-15
相关资源
最近更新 更多