Skip to content

Instantly share code, notes, and snippets.

@christopher-paul-shaw
Created July 19, 2019 08:13
Show Gist options
  • Save christopher-paul-shaw/7d6f269f85756adaf8e1339254dc8cd9 to your computer and use it in GitHub Desktop.
Save christopher-paul-shaw/7d6f269f85756adaf8e1339254dc8cd9 to your computer and use it in GitHub Desktop.
Generating Add Column SQL from Array in php
<?php
$cols = [
'col1',
'col2',
'col3',
'col4',
'col5',
'col6',
'col7',
'col8',
'col9',
'col10',
];
$table = "example";
$after = "col0";
echo "ALTER TABLE `{$table}`".PHP_EOL;
foreach ($array as $col) {
$line_end = end($array) == $col ? ';' : ',';
echo "ADD COLUMN `{$col}` INT(6) NOT NULL DEFAULT 0 AFTER `{$after}`,".PHP_EOL;
$last = $after;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment