JSHINER Posted September 23, 2008 Share Posted September 23, 2008 I have a table - let's call it table_a - I want to add fields to it. I need to add about 30 (a_one, a_two, a_three, b_one, b_two, b_three, etc) - how can I add these with a script instead of doing each individually with phpMyAdmin. One is int( unsigned_zerofill, the rest are text. I need to be able to add for X variable... X_one, X_two, X_three, etc until I tell it to stop. I hope this makes sense. Thanks! Link to comment https://forums.phpfreaks.com/topic/125403-solved-adding-fields-to-a-table/ Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 rough example. you gotta still write the query, but this will get you on the right path: <?php $alpha = array("a", "b", "c", "d", "e", "f"); $numbers = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); foreach ($alpha as $value){ foreach ($numbers as $value2){ echo "run your query here: {$value}_{$value2}\n"; } } Link to comment https://forums.phpfreaks.com/topic/125403-solved-adding-fields-to-a-table/#findComment-648361 Share on other sites More sharing options...
xtopolis Posted September 23, 2008 Share Posted September 23, 2008 Why not be even lazier http://us2.php.net/range <?php $alpha = range('A','Z'); $numbers = range(0,26); ?> Link to comment https://forums.phpfreaks.com/topic/125403-solved-adding-fields-to-a-table/#findComment-648365 Share on other sites More sharing options...
JSHINER Posted September 23, 2008 Author Share Posted September 23, 2008 Thanks guys. Now how do I create those fields in a table that already exists? Link to comment https://forums.phpfreaks.com/topic/125403-solved-adding-fields-to-a-table/#findComment-648373 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 I always forget range. *sigh* The simple ones always slip by me. Link to comment https://forums.phpfreaks.com/topic/125403-solved-adding-fields-to-a-table/#findComment-648375 Share on other sites More sharing options...
trq Posted September 23, 2008 Share Posted September 23, 2008 This sounds more like you need to look into database normalization, adding fileds named a_one, a_two, a_three etc etc seems rediculous. Theres a link in my sig to a book hudzilla, that contains an entire chapters on the basics of normalization, you should take a read. Link to comment https://forums.phpfreaks.com/topic/125403-solved-adding-fields-to-a-table/#findComment-648381 Share on other sites More sharing options...
xtopolis Posted September 23, 2008 Share Posted September 23, 2008 Normalization aside, the command is ALTER: <?php ALTER TABLE `tablename` ADD `ID` INT( 255 ) UNSIGNED ZEROFILL NOT NULL , ADD `A_1` VARCHAR( 255 ) NOT NULL, ADD `B_2` VARCHAR( 255 ) NOT NULL ; ?> But ya, unless you're doing something that you have to do it the way you're asking, I would read up on normalization as well... I'm going to go look at it too ^^ Link to comment https://forums.phpfreaks.com/topic/125403-solved-adding-fields-to-a-table/#findComment-648384 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.