• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
query builder like with wildcard in the middle

#1
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?
Reply

#2
You can use or_like query.

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

#3
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;

Reply

#4
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.
Reply

#5
@plonknimbuzz,

...or you could use query binding ( https://www.codeigniter.com/user_guide/d...y-bindings ). Query binding automatically escapes the variable.

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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.