Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion Tests/Mysqli/MysqliPreparedStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ protected function setUp(): void
)
);
}

$insertQuery = 'INSERT INTO dbtest (title, description, start_date) VALUES (:title, :description, :start_date)';
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $insertQuery);
$mysqliStatementObject->execute([
':title' => 'Test Title',
':description' => 'Test Description',
':start_date' => '2023-01-01',
]);
}

/**
Expand Down Expand Up @@ -148,10 +156,45 @@ public function testPreparedStatementWithSingleKey()
$statement = 'SELECT * FROM dbtest WHERE `title` LIKE :search OR `description` LIKE :search2';
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement);
$dummyValue = 'test';
$dummyValue2 = 'test';
$mysqliStatementObject->bindParam(':search', $dummyValue);
$mysqliStatementObject->bindParam(':search2', $dummyValue);

$mysqliStatementObject->execute();
}

/**
* Regression test to ensure running queries with bound variables still works
*/
public function testPreparedStatementWithBinding()
{
$statement = 'SELECT id FROM dbtest WHERE `title` LIKE :search';
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement);
$title = 'Test Title';
$mysqliStatementObject->bindParam(':search', $title);
$mysqliStatementObject->execute();
$result = $mysqliStatementObject->fetchColumn();

$title = 'changed';
$mysqliStatementObject->execute();
$result2 = $mysqliStatementObject->fetchColumn();
$this->assertNotEquals($result, $result2);
}

/**
* Regression test to ensure running queries with bound variables still works
*/
public function testPreparedStatementWithoutBinding()
{
$statement = 'SELECT id FROM dbtest WHERE `title` LIKE :search';
$mysqliStatementObject = new MysqliStatement(static::$connection->getConnection(), $statement);
$title = 'Test Title';
$params = [':search' => $title];
$mysqliStatementObject->execute($params);
$result = $mysqliStatementObject->fetchColumn();

$params[':search'] = 'changed';
$mysqliStatementObject->execute($params);
$result2 = $mysqliStatementObject->fetchColumn();
$this->assertNotEquals($result, $result2);
}
}
5 changes: 4 additions & 1 deletion src/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@ private function bindValues(array $values)

if (!empty($this->parameterKeyMapping)) {
foreach ($values as $key => &$value) {
$params[$this->parameterKeyMapping[$key]] =& $value;
$paramKey = $this->parameterKeyMapping[$key];
foreach ($paramKey as $currentKey) {
$params[$currentKey] =& $value;
}
}

ksort($params);
Expand Down