Skip to content
Advertisement

Codeigniter LIKE with wildcard(%)

I have simple database query in codeigniter, however I cannot get search to work with wildcard. This is my code:

$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.

Advertisement

Answer

$this->db->like() automatically adds the %s and escapes the string. So all you need is

$this->db->like('title', $query);
$res = $this->db->get('film');

See the CI documentation for reference: CI 2, CI 3, CI 4

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement