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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.