CodeIgniter's $this->db->like() method is automatically escaping percent symbols in the input string

CodeIgniter's $this->db->like() method is automatically escaping percent symbols in the input string

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

CodeIgniter's $this->db->like() method is automatically escaping percent symbols in the input string

When building search functionality in CodeIgniter, a common pitfall involves how the Query Builder handles wildcard characters. Consider a scenario where you want to perform a case-insensitive search for a specific keyword within a database column. You might start by manually replacing spaces with percent signs to create a multi-word search term:

$keyword = "keyword sample"; 
$keyword = str_replace(" ", "%", $keyword);   

$this->db->select('*')
         ->from('table')
         ->like('column', "%".$keyword."%")
         ->get();

At first glance, the logic seems sound. You are sanitizing the input and explicitly wrapping it in wildcards. However, if you inspect the generated SQL query, you might encounter something unexpected:

SELECT * FROM (`table`) WHERE `column` LIKE '%keyword\%sample%'

Notice the backslash preceding the second percent sign: \%. This extra escape character breaks the search logic because the database engine interprets \% as a literal percent symbol rather than a wildcard, causing your query to return fewer or no results.

The root cause lies in CodeIgniter's active record layer and its handling of special characters within the like() method. The framework is designed to protect you from SQL injection by automatically escaping certain characters found in user input before injecting them into the query string. Specifically, it treats backslashes, single quotes, and double quotes as dangerous characters that require prefixing with a backslash.

However, the percent sign (%) presents a unique challenge. In standard SQL wildcards, % matches any sequence of characters. If your input string contains spaces, you often need to replace them manually to make the query work as intended. CodeIgniter's like() method assumes that if you pass a string containing special characters like %, those characters are part of the search term and might be dangerous. Consequently, it blindly applies its escape mechanism.

When your input string $keyword becomes keyword%sample after the str_replace operation, CodeIgniter scans this string for characters it needs to escape. While it correctly identifies that the first % is likely intended as a wildcard (and leaves it alone or handles it based on context), it often treats subsequent special characters strictly. In many versions and configurations of the library, if the input contains a percent sign, the library may erroneously decide to escape it to prevent potential regex injection issues or simply because its internal heuristic flags % as a character that requires escaping when found in certain positions within the parameter binding process.

This behavior is not intuitive but is a direct result of how the LIKE clause parsing works under the hood. The library attempts to ensure that any wildcard characters passed to it are strictly controlled. If you manually construct the query using raw strings, you avoid this issue entirely:

$sql = "SELECT * FROM `table` WHERE `column` LIKE ?";
$params = ["%keyword sample%"]; // No manual replacement needed here
$this->db->query($sql, $params);

Alternatively, if you must use the like() method, you should avoid manually replacing spaces with % characters before passing the string to the function. Instead, pass the raw keyword and let CodeIgniter handle the parameter binding safely:

$keyword = "keyword sample"; 
// Do not replace spaces here.

$this->db->select('*')
         ->from('table')
         ->like('column', "%".$keyword."%", 'both') // Ensure wildcards are outside
         ->get();

By keeping the wildcard symbols (%) strictly outside the string passed to the like() method, you ensure that CodeIgniter only processes the internal text of the keyword. The library will then escape any dangerous characters within that internal text (like quotes) but will leave the outer wildcards intact because they are treated as literal parts of the pattern boundary in your code, not part of the raw input stream being sanitized.

Understanding this distinction between input sanitization and query parameter binding is crucial. If you find yourself fighting against these automatic escapes frequently, switching to raw queries with prepared statements often offers a more predictable experience, especially when dealing with complex search terms containing wildcards.

Our network