catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.83k stars 1.15k forks source link

Like operator does not work #1111

Closed deadboybun closed 7 months ago

deadboybun commented 7 months ago

Information

Describe the Problem The like operator [~] does not work as expected.

Code Snippet The detail code you are using that causes the problem:


##skipped medoo init code
$this->database->select("test_table", "*", [
    'field[~]' => 'abc%'
]);

Expected Behavior the above code should generate and execute the sql "select * from test_table where field like 'abc%'"

Actual Behavior An sql error "SQLSTATE[HY093]: Invalid parameter number: parameter was not defined" occurred

The issue should be related to the fix 1a892c7 of #1109. It seems that the sql statement is not correctly parse by PDO when the parameter placeholder contains a "$".

The issue could be resolved by changing the ending "$" in the placeholder to a "L".

From

$likeClauses[] = $column . ($operator === '!~' ? ' NOT' : '') . " LIKE {$mapKey}L{$index}$";
$map["{$mapKey}L{$index}$"] = [$item, PDO::PARAM_STR];

to

$likeClauses[] = $column . ($operator === '!~' ? ' NOT' : '') . " LIKE {$mapKey}L{$index}L";
$map["{$mapKey}L{$index}L"] = [$item, PDO::PARAM_STR];
catfan commented 7 months ago

Fixed it on 1f035fd.