Skip to content

Commit e56b182

Browse files
Fix MysqliStatement to work with named params (#351)
1 parent 2a9ecd6 commit e56b182

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Tests/Mysqli/MysqliPreparedStatementTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ protected function setUp(): void
5151
)
5252
);
5353
}
54+
55+
$insertQuery = 'INSERT INTO dbtest (title, description, start_date) VALUES (:title, :description, :start_date)';
56+
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $insertQuery);
57+
$mysqliStatementObject->execute([
58+
':title' => 'Test Title',
59+
':description' => 'Test Description',
60+
':start_date' => '2023-01-01',
61+
]);
5462
}
5563

5664
/**
@@ -148,10 +156,45 @@ public function testPreparedStatementWithSingleKey()
148156
$statement = 'SELECT * FROM dbtest WHERE `title` LIKE :search OR `description` LIKE :search2';
149157
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement);
150158
$dummyValue = 'test';
151-
$dummyValue2 = 'test';
152159
$mysqliStatementObject->bindParam(':search', $dummyValue);
153160
$mysqliStatementObject->bindParam(':search2', $dummyValue);
154161

155162
$mysqliStatementObject->execute();
156163
}
164+
165+
/**
166+
* Regression test to ensure running queries with bound variables still works
167+
*/
168+
public function testPreparedStatementWithBinding()
169+
{
170+
$statement = 'SELECT id FROM dbtest WHERE `title` LIKE :search';
171+
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement);
172+
$title = 'Test Title';
173+
$mysqliStatementObject->bindParam(':search', $title);
174+
$mysqliStatementObject->execute();
175+
$result = $mysqliStatementObject->fetchColumn();
176+
177+
$title = 'changed';
178+
$mysqliStatementObject->execute();
179+
$result2 = $mysqliStatementObject->fetchColumn();
180+
$this->assertNotEquals($result, $result2);
181+
}
182+
183+
/**
184+
* Regression test to ensure running queries with bound variables still works
185+
*/
186+
public function testPreparedStatementWithoutBinding()
187+
{
188+
$statement = 'SELECT id FROM dbtest WHERE `title` LIKE :search';
189+
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement);
190+
$title = 'Test Title';
191+
$params = [':search' => $title];
192+
$mysqliStatementObject->execute($params);
193+
$result = $mysqliStatementObject->fetchColumn();
194+
195+
$params[':search'] = 'changed';
196+
$mysqliStatementObject->execute($params);
197+
$result2 = $mysqliStatementObject->fetchColumn();
198+
$this->assertNotEquals($result, $result2);
199+
}
157200
}

src/Mysqli/MysqliStatement.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ private function bindValues(array $values)
306306

307307
if (!empty($this->parameterKeyMapping)) {
308308
foreach ($values as $key => &$value) {
309-
$params[$this->parameterKeyMapping[$key]] =& $value;
309+
$paramKey = $this->parameterKeyMapping[$key];
310+
foreach ($paramKey as $currentKey) {
311+
$params[$currentKey] =& $value;
312+
}
310313
}
311314

312315
ksort($params);

0 commit comments

Comments
 (0)