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
40 changes: 40 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,46 @@ public function update($table, $data, $where)
return $stmt->rowCount();
}


/**
* update record
*
* @param string $table table name
* @param array $data array of columns and values
* @param string $where string of query
* @param array $args params of query
*/
public function updateByWhere($table, $data, $where, $args = [])
{
//collect the values from data and where
$values = [];

//setup fields
$fieldDetails = '';
foreach ($data as $key => $value) {
$key = '`' . trim($key, '`') . '`';
$fieldDetails .= "$key = ?,";
$values[] = $value;
}
$fieldDetails = rtrim($fieldDetails, ',');

//setup where
$where = trim($where);
$whereDetails = $where ?: '';
$values = array_merge($values, $args);

// build SQL statement
$sql = "UPDATE $table SET $fieldDetails";
if ($whereDetails) {
$sql .= " WHERE $whereDetails";
}

//execute query
$stmt = $this->run($sql, $values);

return $stmt->rowCount();
}

/**
* Delete records
*
Expand Down