Jam*_*las 23 mysql sql activerecord codeigniter
我在codeigniter中有简单的数据库查询,但是我无法使用通配符进行搜索.这是我的代码:
$this->db->like('film.title',"%$query%");
$this->db->escape_like_str($query);
$res = $this->db->get('film');
Run Code Online (Sandbox Code Playgroud)
如果我删除通配符(%),那么搜索工作正常.$ query也只是用户输入的字符串.任何帮助表示赞赏.
Sam*_*utz 41
$this->db->like()自动添加%s并转义字符串.所以你需要的只是
$this->db->like('title', $query);
$res = $this->db->get('film');
Run Code Online (Sandbox Code Playgroud)
请参阅CI文档中的Active Record Class(CI 2)或Query Builder Class(CI 3)以供参考.
小智 9
毛皮满就像你可以用户:
$this->db->like('title',$query);
Run Code Online (Sandbox Code Playgroud)
对于%$查询,您可以使用
$this->db->like('title', $query, 'before');
Run Code Online (Sandbox Code Playgroud)
并且对于$ query%,您可以使用
$this->db->like('title', $query, 'after');
Run Code Online (Sandbox Code Playgroud)
小智 9
$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%'
Run Code Online (Sandbox Code Playgroud)
小智 6
$this->db->like()
此方法使您能够生成 LIKE 子句,这对进行搜索很有用。
$this->db->like('title', 'match');
产生:WHERE titleLIKE '%match%'
如果要控制放置通配符 (%) 的位置,可以使用可选的第三个参数。您的选项是“之前”、“之后”、“无”和“两者”(这是默认值)。
$this->db->like('title', 'match', 'before');
产生:WHERE titleLIKE '%match'
$this->db->like('title', 'match', 'after');
产生:WHERE titleLIKE 'match%'
$this->db->like('title', 'match', 'none');
产生:WHERE titleLIKE 'match'
$this->db->like('title', 'match', 'both');
产生:WHERE titleLIKE '%match%'
| 归档时间: |
|
| 查看次数: |
109337 次 |
| 最近记录: |