diff --git a/system/Database/Query.php b/system/Database/Query.php index 650a7da93193..dae2eeb11849 100644 --- a/system/Database/Query.php +++ b/system/Database/Query.php @@ -368,7 +368,7 @@ protected function compileBinds() { $sql = $this->finalQueryString; - $hasNamedBinds = strpos($sql, ':') !== false; + $hasNamedBinds = strpos($sql, ':') !== false && strpos($sql, ':=') === false; if (empty($this->binds) || empty($this->bindMarker) || (strpos($sql, $this->bindMarker) === false && diff --git a/tests/system/Database/BaseQueryTest.php b/tests/system/Database/BaseQueryTest.php index 7a92467c3618..4e09d49ccc24 100644 --- a/tests/system/Database/BaseQueryTest.php +++ b/tests/system/Database/BaseQueryTest.php @@ -316,4 +316,23 @@ public function testSetQueryBindsWithSetEscapeFalse() $this->assertEquals($expected, $query->getQuery()); } + + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/2762 + */ + public function testSetQueryBinds() + { + $query = new Query($this->db); + + $binds = [ + 1, + 2, + ]; + + $query->setQuery('SELECT @factorA := ?, @factorB := ?', $binds); + + $expected = 'SELECT @factorA := 1, @factorB := 2'; + + $this->assertEquals($expected, $query->getQuery()); + } }