Skip to content

Commit 130bd5d

Browse files
Copilotthorsten
andcommitted
fix: SQL syntax error with LIKE ESCAPE clause - use double backslash in escape sequence
Agent-Logs-Url: https://github.com/thorsten/phpMyFAQ/sessions/fb97f7b6-1d3a-49ea-9c67-aa74beee56f2 Co-authored-by: thorsten <45284+thorsten@users.noreply.github.com>
1 parent 09343d6 commit 130bd5d

4 files changed

Lines changed: 6 additions & 6 deletions

File tree

phpmyfaq/src/phpMyFAQ/Search.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ private function searchCustomPages(string $searchTerm, bool $allLanguages = true
249249
// Escape LIKE metacharacters (%, _) to prevent wildcard injection
250250
$escapedWord = str_replace(['\\', '%', '_'], ['\\\\', '\\%', '\\_'], $word);
251251
$searchConditions[] = sprintf(
252-
"(page_title LIKE '%%%s%%' ESCAPE '\\' OR content LIKE '%%%s%%' ESCAPE '\\')",
252+
"(page_title LIKE '%%%s%%' ESCAPE '\\\\' OR content LIKE '%%%s%%' ESCAPE '\\\\')",
253253
$escapedWord,
254254
$escapedWord,
255255
);

phpmyfaq/src/phpMyFAQ/Search/Database/PdoPgsql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function search(string $searchTerm): mixed
7474
FROM
7575
%s %s %s %s
7676
WHERE
77-
(%s) ILIKE ('%%%s%%') ESCAPE '\\'
77+
(%s) ILIKE ('%%%s%%') ESCAPE '\\\\'
7878
%s
7979
%s",
8080
$columns,

phpmyfaq/src/phpMyFAQ/Search/SearchDatabase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public function getMatchClause(string $searchTerm = ''): string
264264
}
265265

266266
$where = sprintf(
267-
"%s%s LIKE '%%%s%%' ESCAPE '\\'",
267+
"%s%s LIKE '%%%s%%' ESCAPE '\\\\'",
268268
$where,
269269
$this->matchingColumns[$j],
270270
self::escapeLikeWildcards($this->configuration->getDb()->escape($keys[$i])),

tests/phpMyFAQ/Search/SearchDatabaseTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function testGetMatchClause()
149149
{
150150
$this->searchDatabase->setMatchingColumns(['faqdata.author']);
151151
$this->assertEquals(
152-
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\')",
152+
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\\\')",
153153
$this->searchDatabase->getMatchClause('Thorsten'),
154154
);
155155
$this->assertIsString($this->searchDatabase->getMatchClause('Thorsten'));
@@ -159,7 +159,7 @@ public function testGetMatchClauseWithTwoSearchTerms()
159159
{
160160
$this->searchDatabase->setMatchingColumns(['faqdata.author']);
161161
$this->assertEquals(
162-
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\') OR (faqdata.author LIKE '%Rinne%' ESCAPE '\\')",
162+
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\\\') OR (faqdata.author LIKE '%Rinne%' ESCAPE '\\\\')",
163163
$this->searchDatabase->getMatchClause('Thorsten Rinne'),
164164
);
165165
$this->assertIsString($this->searchDatabase->getMatchClause('Thorsten'));
@@ -169,7 +169,7 @@ public function testGetMatchClauseWithTwoColumns()
169169
{
170170
$this->searchDatabase->setMatchingColumns(['faqdata.author', 'faqdata.thema']);
171171
$this->assertEquals(
172-
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\' OR faqdata.thema LIKE '%Thorsten%' ESCAPE '\\')",
172+
" (faqdata.author LIKE '%Thorsten%' ESCAPE '\\\\' OR faqdata.thema LIKE '%Thorsten%' ESCAPE '\\\\')",
173173
$this->searchDatabase->getMatchClause('Thorsten'),
174174
);
175175
$this->assertIsString($this->searchDatabase->getMatchClause('Thorsten'));

0 commit comments

Comments
 (0)