Skip to content
Open
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
57 changes: 20 additions & 37 deletions src/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class MysqliStatement implements StatementInterface
* @var array
* @since 2.0.0
*/
protected $bindedValues;
protected $bindedValues = [];

/**
* Mapping between named parameters and position in query.
*
* @var array
* @since 2.0.0
*/
protected $parameterKeyMapping;
protected $parameterKeyMapping = [];

/**
* Mapping array for parameter types.
Expand Down Expand Up @@ -301,27 +301,23 @@ public function bindParam($parameter, &$variable, string $dataType = ParameterTy
*/
private function bindValues(array $values)
{
$params = [];
$types = str_repeat('s', \count($values));

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

ksort($params);
} else {
foreach ($values as $key => &$value) {
$params[] =& $value;
}
}

array_unshift($params, $types);
return $this->statement->bind_param($types, ...$params);
}

return \call_user_func_array([$this->statement, 'bind_param'], $params);
return $this->statement->bind_param($types, ...$values);
}

/**
Expand Down Expand Up @@ -372,32 +368,30 @@ public function errorInfo()
*/
public function execute(?array $parameters = null)
{
if ($this->bindedValues !== null) {
if ($this->bindedValues !== []) {
$params = [];
$types = [];

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

foreach ($paramKey as $currentKey) {
$params[$currentKey] =& $value;
$params[$currentKey] = $value;
$types[$currentKey] = $this->typesKeyMapping[$key];
}
}
} else {
foreach ($this->bindedValues as $key => &$value) {
$params[] =& $value;
foreach ($this->bindedValues as $key => $value) {
$params[] = $value;
$types[$key] = $this->typesKeyMapping[$key];
}
}

ksort($params);
ksort($types);

array_unshift($params, implode('', $types));

if (!\call_user_func_array([$this->statement, 'bind_param'], $params)) {
if (!$this->statement->bind_param(implode('', $types), ...$params)) {
throw new PrepareStatementFailureException($this->statement->error, $this->statement->errno);
}
} elseif ($parameters !== null) {
Expand All @@ -418,15 +412,7 @@ public function execute(?array $parameters = null)
$meta = $this->statement->result_metadata();

if ($meta !== false) {
$columnNames = [];

foreach ($meta->fetch_fields() as $col) {
$columnNames[] = $col->name;
}

$meta->free();

$this->columnNames = $columnNames;
$this->columnNames = array_column($meta->fetch_fields(), 'name');
} else {
$this->columnNames = false;
}
Expand All @@ -436,13 +422,10 @@ public function execute(?array $parameters = null)
$this->statement->store_result();

$this->rowBindedValues = array_fill(0, \count($this->columnNames), null);
$refs = [];

foreach ($this->rowBindedValues as $key => &$value) {
$refs[$key] =& $value;
}
// The following is necessary as PHP cannot handle references to properties properly
$refs =& $this->rowBindedValues;

if (!\call_user_func_array([$this->statement, 'bind_result'], $refs)) {
if (!$this->statement->bind_result(...$refs)) {
throw new \RuntimeException($this->statement->error, $this->statement->errno);
}
}
Expand Down