phpcodeigniteractiverecordwildcardsql-like

CodeIgniter active record like() is escaping manually added wildcards (%)


I cannot get my database query to work with wildcards.

$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 a string with user input.


Solution

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