Jump to content

[SOLVED] Adding Fields to a Table


JSHINER

Recommended Posts

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(8) 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

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";
}
}

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.

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 ^^

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.