【发布时间】:2015-07-25 13:57:40
【问题描述】:
假设我有这样的功能:
public function get_list($category = '', $limit = 10, $offset = 0) {
if (!empty($category))
$this->db->where('category', $category);
$search = $this->input->get('search');
if (!empty($search))
$this->db->or_like(array('foo_column'=>$search));
$query = $this->db->get('table_name', $limit, $offset);
//echo $this->db->last_query();
return $query->result();
}
产生查询:
SELECT * FROM table_name WHERE foo_column LIKE '%match something%'
如您所见,% 通配符可以添加到两侧,before 和 after。
如果我想像这样制作:
... WHERE foo_column LIKE '%match%something%'?
仅供参考,我使用 str_replace() 函数到 change space 到 % 但 codeigniter 始终将 escape 与 slash 一起使用。
它产生如下查询:
... WHERE foo_column LIKE '%match\%something%'
当使用关键字 match something 搜索 match another something 并且 wildcard 在第一个和/或之后似乎不起作用时,这很有用。
【问题讨论】:
标签: php mysql codeigniter query-builder