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

(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");
The output of this function is:
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:
SELECT * FROM `db_my_table` WHERE col1 LIKE '20%'
whereas an $escape value of true
PHP Code:
yields this:
SELECT * FROM `db_my_table` WHERE `col1` LIKE '20%' ESCAPE '!'
when I thought it would escape the special wildcard char % like so:
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

In CI3,
PHP Code:
$sql $this->db->get_compiled_select("my_table"); 

FROM `my_table`
WHERE  col1  LIKE '20%'

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

Theme © iAndrew 2016 - Forum software by © MyBB