Reham Fahmy
Reham Fahmy

Reputation: 5073

Update database rows

Let us say we've database table my_table with the following structure

CREATE TABLE `my_table` (
`title` text,
`name` text
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `admin` VALUES ('my title', 'my name');

Now i want to update it to be with 1 extra field slogan to be like this

CREATE TABLE `my_table` (
`title` text,
`name` text,
`slogan` text
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `admin` VALUES ('my title', 'my name', 'my slogan');

For sure if it 10 or even 100 i can do it using regex with any text editor but i it too huge for my processor to do it so i've been thinking to use PHP to update my table while it on my hosting server.

I've been thinking to use

$q1 = "CREATE TABLE `my_table`(
`slogan` text )";
mysql_query($q1) or die(mysql_error()." at row ".__LINE__);

but given me error my_table already exist so any help ~ thanks

Upvotes: 0

Views: 65

Answers (3)

user319198
user319198

Reputation:

You are using CREATE TABLE that is for creating new table but as you saying you already created table and just want to add new column slogan. So you need to use ALTER Table syntax.

So use alter query as described in other posts:

ALTER TABLE `my_table` ADD COLUMN `slogan` text

Upvotes: 1

Eugen Rieck
Eugen Rieck

Reputation: 65334

What you need is

ALTER TABLE `my_table` ADD COLUMN `slogan` text;

followed by lots of

UPDATE `my_table` SET `slogan`='...' WHERE `title`=' ... ';

Upvotes: 1

Niko Sams
Niko Sams

Reputation: 4414

Try that:

ALTER TABLE my_table ADD slogan text;

Upvotes: 2

Related Questions