diff --git a/system/Database/SQLSRV/PreparedQuery.php b/system/Database/SQLSRV/PreparedQuery.php index 5e9c28913ad2..425555927c60 100644 --- a/system/Database/SQLSRV/PreparedQuery.php +++ b/system/Database/SQLSRV/PreparedQuery.php @@ -131,7 +131,11 @@ protected function parameterize(string $queryString, array $options): array for ($c = 0; $c < $numberOfVariables; $c++) { $this->parameters[$c] = null; - $params[] = [&$this->parameters[$c], SQLSRV_PARAM_IN, $options[$c] ?? SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR)]; + if (isset($options[$c])) { + $params[] = [&$this->parameters[$c], SQLSRV_PARAM_IN, $options[$c]]; + } else { + $params[] = &$this->parameters[$c]; + } } return $params; diff --git a/tests/system/Database/Live/PreparedQueryTest.php b/tests/system/Database/Live/PreparedQueryTest.php index 5513b2ee6bd2..80e0addbca82 100644 --- a/tests/system/Database/Live/PreparedQueryTest.php +++ b/tests/system/Database/Live/PreparedQueryTest.php @@ -284,7 +284,11 @@ public function testInsertBinaryData(): void $fileContent = file_get_contents(TESTPATH . '_support/Images/EXIFsamples/landscape_0.jpg'); $this->assertTrue($this->query->execute($fileContent)); - $id = $this->db->insertID(); + $id = $this->db->DBDriver === 'SQLSRV' + // It seems like INSERT for a prepared statement is run in the + // separate execution context even though it's part of the same session + ? (int) ($this->db->query('SELECT @@IDENTITY AS insert_id')->getRow()->insert_id ?? 0) + : $this->db->insertID(); $builder = $this->db->table('type_test'); if ($this->db->DBDriver === 'Postgre') {