Skip to content

Instantly share code, notes, and snippets.

@donmccurdy
Last active December 24, 2015 13:19
Show Gist options
  • Save donmccurdy/6803709 to your computer and use it in GitHub Desktop.
Save donmccurdy/6803709 to your computer and use it in GitHub Desktop.
Batch Query Updates (Example)
////// UPDATE APP DATA TABLE.
$old_app_data = db_query("SELECT key_id, value FROM {%s} WHERE data_id=%d ORDER BY key_id ASC;",
$def->app_data_table, $def->app_data_id);
$i = 1001;
$updates = [];
$mapping = [];
while ($row = db_fetch_array($old_app_data)) {
$mapping[$row['key_id']] = [
'old_key_id' => $row['key_id'],
'new_key_id' => $i,
'value' => $row['value']
];
$updates[] = sprintf("WHEN '%s' THEN %s", $row['key_id'], $i);
$i++;
}
$query = sprintf(
"UPDATE {%s}
SET key_id = CASE key_id
%s
END
WHERE data_id=%d;",
$def->app_data_table,
implode(" \n ", $updates),
$def->app_data_id
);
$app_data_success = db_query($query);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment