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 in WHERE wrong statement with dash (-) #1001

Closed objectivisto closed 2 years ago

objectivisto commented 3 years ago

Medoo’s count() in version 2.1.2 is generating a wrong SQL statement, if we use the LIKE operator in the WHERE clause, and the search term contains a dash. Under these conditions, it is missing the embedding % character at the start and the end of the search term.

This works well:

$searchString = "tomspage”; // no dash in search term $countAds = $db->count('xyzAd', array('linkManual[~]'=>$searchString)); Correct query generated: SELECT COUNT(*) FROM xyzAd WHERE (linkManual LIKE '%tomspage%')

This does not work:

$searchString = “toms-page"; // with dash in search term $countAds = $db->count('xyzAd', array('linkManual[~]'=>$searchString)); Faulty query generated (missing %%): SELECT COUNT(*) FROM xyzAd WHERE (linkManual LIKE 'toms-page')

In previous versions of Medoo it works well, verified in version 1.7.10.

Seevil commented 2 years ago

I also encountered this problem...

Seevil commented 2 years ago

xdebug 分析后发现可能是正则的问题 https://github.com/catfan/Medoo/blob/8d2147808a2795cf58f6da94f82281f6a5433221/src/Medoo.php#L1171

执行到此行 LIKE 数值丢失😫

axelhahn commented 2 years ago

I have the problem in a select() too ... if there is a LIKE statement 'mycolumn[~]' => $value and $value contains a dash then $value doesn' get surrounding % chars in the query.

catfan commented 2 years ago

Fixed it on 66ddd62.

webgoto commented 2 years ago

The behavior is also strange when it contains underscores.

$database->select('options', 'option_id', ['option_id[~]'=>'a_']); ↓ SELECT `option_id` FROM `options` WHERE (`optionid` LIKE 'a')

'a_' is not wrapped in '%'.


It works fine if I escape with '\'.

$database->select('options', 'option_id', ['option_id[~]'=>'a\_']); ↓ SELECT `option_id` FROM `options` WHERE (`optionid` LIKE '%a\%')

Is this bug?

medoo version 2.1.3

catfan commented 2 years ago

@webgoto _ is a wildcard character for representing a single character. a_ will match aa, ab, ac, and more.

So if you just want to match the underscore _ character, you need to add a slash.

https://www.w3schools.com/sql/sql_wildcards.asp