diff --git a/htdocs/core/db/mysqli.class.php b/htdocs/core/db/mysqli.class.php index e36fc547fa0..7d2f075944b 100644 --- a/htdocs/core/db/mysqli.class.php +++ b/htdocs/core/db/mysqli.class.php @@ -484,7 +484,7 @@ class DoliDBMysqli extends DoliDB */ public function escapeforlike($stringtoencode) { - return str_replace(array('_', '\\', '%'), array('\_', '\\\\', '\%'), (string) $stringtoencode); + return str_replace(array('\\', '_', '%'), array('\\\\', '\_', '\%'), (string) $stringtoencode); } /** diff --git a/htdocs/core/db/pgsql.class.php b/htdocs/core/db/pgsql.class.php index fb18ed0f161..e7e0b0da9d8 100644 --- a/htdocs/core/db/pgsql.class.php +++ b/htdocs/core/db/pgsql.class.php @@ -729,7 +729,7 @@ class DoliDBPgsql extends DoliDB */ public function escapeforlike($stringtoencode) { - return str_replace(array('_', '\\', '%'), array('\_', '\\\\', '\%'), (string) $stringtoencode); + return str_replace(array('\\', '_', '%'), array('\\\\', '\_', '\%'), (string) $stringtoencode); } /** diff --git a/htdocs/core/db/sqlite3.class.php b/htdocs/core/db/sqlite3.class.php index 40d0f10baa8..819e5ba72f0 100644 --- a/htdocs/core/db/sqlite3.class.php +++ b/htdocs/core/db/sqlite3.class.php @@ -657,7 +657,7 @@ class DoliDBSqlite3 extends DoliDB */ public function escapeforlike($stringtoencode) { - return str_replace(array('_', '\\', '%'), array('\_', '\\\\', '\%'), (string) $stringtoencode); + return str_replace(array('\\', '_', '%'), array('\\\\', '\_', '\%'), (string) $stringtoencode); } /** diff --git a/test/phpunit/CodingSqlTest.php b/test/phpunit/CodingSqlTest.php index 00c7a5aebae..504203eeabf 100644 --- a/test/phpunit/CodingSqlTest.php +++ b/test/phpunit/CodingSqlTest.php @@ -157,6 +157,44 @@ class CodingSqlTest extends PHPUnit\Framework\TestCase print __METHOD__."\n"; } + /** + * testEscape + * + * @return string + */ + public function testEscape() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $a = 'abc"\'def'; + print $a; + $result = $db->escape($a); // $result must be abc\"\'def + $this->assertEquals('abc\"\\\'def', $result); + } + + /** + * testEscapeForLike + * + * @return string + */ + public function testEscapeForLike() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + $a = 'abc"\'def_ghi%klm\\nop'; + //print $a; + $result = $db->escapeforlike($a); // $result must be abc"'def\_ghi\%klm\\nop + $this->assertEquals('abc"\'def\_ghi\%klm\\\\nop', $result); + } + /** * testSql *