Welcome Guest, Not a member yet? Register   Sign In
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

(This post was last modified: 12-24-2017, 06:38 AM by Wouter60.)

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




Theme © iAndrew 2016 - Forum software by © MyBB