Welcome Guest, Not a member yet? Register   Sign In
what does fourth parameter ($escape) actually do for $builder->like?
#1

(This post was last modified: 02-24-2021, 05:33 PM by sneakyimp.)

I was expecting that the fourth parameter for the Builder::like function would cause special characters in the $match parameter to be escaped. I was quite surprised to see that supplying FALSE for this parameter results in no quotation marks for the $match parameter. Is this a bug?

An example. I would expect the % in 'H%' to be escaped:

PHP Code:
    function test2() {
        
$db = \Config\Database::connect("tests");
        
$builder $db->table("my_table");
        
$builder->like('col1''20%''none'false);
        die(
$builder->testMode()->getCompiledSelect());
    } 
The output of this function is:
Code:
SELECT * FROM `db_my_table` WHERE col1 LIKE 20%
whereas I was expecting an $escape of false to suppress escaping of special characters, perhaps yielding something like this:
Code:
SELECT * FROM `db_my_table` WHERE col1 LIKE '20%'
whereas an $escape value of true
PHP Code:
        $builder->like('col1''20%''none'true); 
yields this:
Code:
SELECT * FROM `db_my_table` WHERE `col1` LIKE '20%' ESCAPE '!'
when I thought it would escape the special wildcard char % like so:
Code:
SELECT * FROM `db_my_table` WHERE col1 LIKE '20!%' ESCAPE '!'

NOTE that in every case the table is surrounded by backticks, and the $escape parameter turns on/off the backticks around the column name and the quotes around the pattern to be matched.

Can someone explain to me what the $escape parameter is really doing? The documentation is not very clear at all:
Quote:$escape (bool) – Whether to escape values and identifiers
Reply
#2

In CI3,
PHP Code:
$this->db->like('col1''20%''none'false);
$sql $this->db->get_compiled_select("my_table"); 

Code:
SELECT *
FROM `my_table`
WHERE  col1  LIKE '20%'

At least, the following SQL is invalid, and it must be a bug in CI4.
Code:
SELECT * FROM `db_my_table` WHERE col1 LIKE 20%
Reply




Theme © iAndrew 2016 - Forum software by © MyBB