codeigniter 3.0 like query add ESCAPE '!' on like

3.3k views Asked by At

codeigniter 3.0 like query add ESCAPE '!'

example:

 $this->db->select('*');
    $this->db->from('sample');
    $this->db->like('col','val%val2');
    $this->db->get()->result_array();

CI produces the query like following

SELECT * FROM `sample` WHERE `col` LIKE '%val!%val2%' ESCAPE '!'

But I expected it

SELECT * FROM `sample` WHERE `col` LIKE '%val%val2%' 

How can I achieve that?

2

There are 2 answers

1
Shaiful Islam On

AT CI-2 it produce the result as expected.But CI-3 Producing result as you said.May be it should be improve.

After some research I found solution where you need to set some config inside system/database/DB_driver.php

Open the the DB_driver.php go to line 340 and 347. You will find like this

protected $_like_escape_str = " ESCAPE '%s' ";

/**
 * ESCAPE character
 *
 * @var string
 */
protected $_like_escape_chr = '!';

Change them to empty like following

protected $_like_escape_str = "";

/**
 * ESCAPE character
 *
 * @var string
 */
protected $_like_escape_chr = '';
0
shubomb On

After searching I found that the answer, it will remove '!' ESCAPE sign from query

$this->db->like('category_name', $string,'both',false);