Skip to content

Commit 09b0caf

Browse files
authored
Merge pull request #19 from DizzySquirrel/master
Update Database.php
2 parents 8edb194 + d3455fa commit 09b0caf

1 file changed

Lines changed: 27 additions & 11 deletions

File tree

src/Database.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,25 @@ public function run($sql, $args = [])
7676
}
7777

7878
$stmt = $this->db->prepare($sql);
79-
$stmt->execute($args);
80-
79+
80+
//check if args is associative or sequential?
81+
$is_assoc = (array() === $args) ? false : array_keys($args) !== range(0, count($args) - 1);
82+
if ($is_assoc)
83+
{
84+
foreach ($args as $key => $value) {
85+
if (is_int($value)) {
86+
$stmt->bindValue(":$key", $value, PDO::PARAM_INT);
87+
} else {
88+
$stmt->bindValue(":$key", $value);
89+
}
90+
}
91+
$stmt->execute();
92+
}
93+
else
94+
{
95+
$stmt->execute($args);
96+
}
97+
8198
return $stmt;
8299
}
83100

@@ -176,29 +193,28 @@ public function insert($table, $data)
176193
*/
177194
public function update($table, $data, $where)
178195
{
179-
//merge data and where together
180-
$collection = array_merge($data, $where);
181-
182-
//collect the values from collection
183-
$values = array_values($collection);
184-
196+
//collect the values from data and where
197+
$values = [];
198+
185199
//setup fields
186200
$fieldDetails = null;
187201
foreach ($data as $key => $value) {
188202
$fieldDetails .= "$key = ?,";
203+
$values[] = $value;
189204
}
190205
$fieldDetails = rtrim($fieldDetails, ',');
191-
206+
192207
//setup where
193208
$whereDetails = null;
194209
$i = 0;
195210
foreach ($where as $key => $value) {
196211
$whereDetails .= $i == 0 ? "$key = ?" : " AND $key = ?";
212+
$values[] = $value;
197213
$i++;
198214
}
199-
215+
200216
$stmt = $this->run("UPDATE $table SET $fieldDetails WHERE $whereDetails", $values);
201-
217+
202218
return $stmt->rowCount();
203219
}
204220

0 commit comments

Comments
 (0)