通用更新資料表資料方法
阿新 • • 發佈:2018-12-10
yii2.0適用
function updateTableData($tableName, $data, $whereArr)
{
//更新資料組合
$set = [];
foreach ($data as $key => $val) {
if ($key != 'id') {
$set[] = "`{$key}` = '{$val}'";
}
}
$set = implode(",", $set);
//更新條件組合
$where = [];
foreach ($whereArr as $key => $val) {
$where[] = " `{$key}` = '{$val}' ";
}
$where = implode("and", $where);
$sql = "UPDATE `{$tableName}` SET {$set} WHERE {$where} ";
return Yii::$app->getDb()->createCommand($sql)->execute();
}