Created
July 19, 2019 08:13
-
-
Save christopher-paul-shaw/7d6f269f85756adaf8e1339254dc8cd9 to your computer and use it in GitHub Desktop.
Generating Add Column SQL from Array in php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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