【问题标题】:CodeIgniter Active Record 'like' ignoring 'where'CodeIgniter Active Record 'like' 忽略 'where'
【发布时间】:2012-06-25 10:45:44
【问题描述】:

使用 like 进行非常简单的搜索,并且可以选择省略选项,但是我发现 like 语句使查询忽略 where 语句

$this->db->like('LOWER(location) OR LOWER(name)', strtolower($term));
$this->db->where('stage', 1);
$this->db->order_by("name", "asc"); 
$query = $this->db->get($this->user_table);
return $query->result();

上面使用 $term = "dublin"; 产生的示例;

SELECT * FROM (`users`) WHERE `stage` = 1 AND LOWER(location) OR LOWER(name) LIKE '%dublin%' ORDER BY `name` asc"

它仍然返回 'stage' 不等于 1 的行。

有什么想法吗?谢谢!

【问题讨论】:

标签: php codeigniter activerecord


【解决方案1】:
$term = strtolower($term);
$this->db->where("(LOWER(location) LIKE '%{$term}%' OR LOWER(name) LIKE '%{$term}%')");

【讨论】:

    【解决方案2】:
    $query = $this->db->query("SELECT id_alimento, nombre, unidad, energia_kcal FROM catalogo_alimentos WHERE LOWER(nombre) LIKE '%".$this->db->escape_like_str($search)."%'");
    if($query != false){
      if ($query->num_rows() > 0) {
        return $query->result();
      }
    }
    

    【讨论】:

    • 请在此处提供一些信息,说明为什么这是一个答案。
    【解决方案3】:

    用这个代替查询

    $term = strtolower($term);
    $this->db->where("stage= 1 AND (LOWER(location) LIKE '%{$term}%' OR LOWER(name) LIKE '%{$term}%')");
    

    【讨论】:

    • 请不要只给出代码答案。解释您的代码的作用,以及它如何满足 OP 要求。
    猜你喜欢
    • 1970-01-01
    • 2012-08-12
    • 2013-02-27
    • 2015-09-17
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 2017-03-30
    • 2017-11-30
    相关资源
    最近更新 更多