MySQL Codeigniter LIKE 带通配符(%)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16226906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter LIKE with wildcard(%)
提问by James Douglas
I have simple database query in codeigniter, however I cannot get search to work with wildcard. This is my code:
我在 codeigniter 中有简单的数据库查询,但是我无法使用通配符进行搜索。这是我的代码:
$this->db->like('film.title',"%$query%");
$this->db->escape_like_str($query);
$res = $this->db->get('film');
If I remove wildcard(%) then search works fine. Also $query is just string with user input. Any help is appreciated.
如果我删除通配符(%) 那么搜索工作正常。另外 $query 只是带有用户输入的字符串。任何帮助表示赞赏。
回答by Samutz
回答by NIMESH PAREKH
For Full like you can user :
对于完整的用户,您可以使用:
$this->db->like('title',$query);
For %$query you can use
对于 %$query,您可以使用
$this->db->like('title', $query, 'before');
and for $query% you can use
对于 $query% 你可以使用
$this->db->like('title', $query, 'after');
回答by Bijendra Ch
$this->db->like('title', 'match', 'before');
// Produces: WHERE title LIKE '%match'
$this->db->like('title', 'match', 'after');
// Produces: WHERE title LIKE 'match%'
$this->db->like('title', 'match', 'both');
// Produces: WHERE title LIKE '%match%'
回答by symi khan
$this->db->like()
$this->db->like()
This method enables you to generate LIKE clauses, useful for doing searches.
此方法使您能够生成 LIKE 子句,这对进行搜索很有用。
$this->db->like('title', 'match');
$this->db->like('title', 'match');
Produces: WHERE title
LIKE '%match%'
产生:WHERE title
LIKE '%match%'
If you want to control where the wildcard (%) is placed, you can use an optional third argument. Your options are ‘before', ‘after', ‘none' and ‘both' (which is the default).
如果要控制放置通配符 (%) 的位置,可以使用可选的第三个参数。您的选项是“之前”、“之后”、“无”和“两者”(这是默认值)。
$this->db->like('title', 'match', 'before');
$this->db->like('title', 'match', 'before');
Produces: WHERE title
LIKE '%match'
产生:WHERE title
LIKE '%match'
$this->db->like('title', 'match', 'after');
$this->db->like('title', 'match', 'after');
Produces: WHERE title
LIKE 'match%'
产生:WHERE title
LIKE 'match%'
$this->db->like('title', 'match', 'none');
$this->db->like('title', 'match', 'none');
Produces: WHERE title
LIKE 'match'
产生:WHERE title
LIKE 'match'
$this->db->like('title', 'match', 'both');
$this->db->like('title', 'match', 'both');
Produces: WHERE title
LIKE '%match%'
产生:WHERE title
LIKE '%match%'
回答by jemrlee
If you do not want to use the wildcard (%) you can pass to the optional third argument the option 'none'.
如果您不想使用通配符 (%),您可以将选项“none”传递给可选的第三个参数。
$this->db->like('title', 'match', 'none');
// Produces: WHERE title LIKE 'match'
回答by Spec
I'm using
我正在使用
$this->db->query("SELECT * FROM film WHERE film.title LIKE '%$query%'"); for such purposes
回答by CG_DEV
Searching multiple fields at once can be done like so...
可以像这样一次搜索多个字段......
function search($search)
{
$sql = "SELECT * FROM some_table
WHERE UPPER(a_name) LIKE ?
OR
UPPER(a_full_name) LIKE ?
OR
UPPER(a_city) LIKE ?
OR
UPPER(a_code) LIKE ?
";
$arr = array_map(array($this,"wrapLIKE"),array($search, $search, $search, $search));
$r = $this->db->query($sql, $arr);
return $r;
}
function wrapLIKE($string)
{
return strtoupper("%$string%");
}