CodeIgniter Forums
query builder like with wildcard in the middle - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: query builder like with wildcard in the middle (/showthread.php?tid=69603)



query builder like with wildcard in the middle - plonknimbuzz - 12-20-2017

Code:
SELECT * FROM table WHERE column LIKE 'some%thing'


3rd parameter
after = 'some%thing%'
before = '%some%thing'
both (or leave empty) = '%some%thing%'

but how with 'some%thing' ??

i just can use this:
Code:
SELECT * FROM table WHERE column LIKE '%" .$this->db->escape_like_str($search)."%' ESCAPE '!


can i use query builder to produce LIKE clause with wildcare in the middle?


RE: query builder like with wildcard in the middle - XtreemDeveloper - 12-23-2017

You can use or_like query.

$alpha = $searchData['alpha'];
$where = "`title` LIKE '$alpha%'";
$this->db->where($where);


RE: query builder like with wildcard in the middle - Wouter60 - 12-23-2017

The query builder works differently:
PHP Code:
$this->db->like('column'$alpha'both'); 
$query $this->db->get('table');

if (
$query->num_rows() > 0) {
 
  $records $query->result();
}
else {
 
  $records NULL;




RE: query builder like with wildcard in the middle - mdr1384 - 06-13-2019

You can use the existing 3rd and 4th parameters to like():

Code:
$this->db->like('column', 'some%thing', 'none', FALSE)

3rd param prevents prefix/postfix additional wildcard chars, 4th param prevents escaping embedded wildcard chars.


RE: query builder like with wildcard in the middle - php_rocs - 06-13-2019

@plonknimbuzz,

...or you could use query binding ( https://www.codeigniter.com/user_guide/database/queries.html#query-bindings ). Query binding automatically escapes the variable.

$sql = "SELECT * FROM table WHERE column LIKE '%?%' ESCAPE!";
$this->db->query($sql, $value));