Jump to content

Add a new column to mysql table


kristian_gl

Recommended Posts

Hello.

 

I am trying to add a new column to "table1" in Mysql database. I suspect that there is a syntax problem with my sql query. The closest I have been is, creating a new column with the name "$row1", opposed to the giving the column the name of the value with in the variable $row1. And yes, I have tried to echo out $row1 to verify that it's not empty.

 

$sql = "ALTER TABLE `table1` ADD ".$row1." VARCHAR(320) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL;";

mysql_query($sql);

 

Using:

PHP version: 5.2.11

Mysql version: 5.1.36

 

Kristian

Link to comment
Share on other sites

yes,  it does.

 

I have now made some progress, but i'm not there yet.

 

CODE:

while($row = mysql_fetch_array($result))

{

 

$row1 = $row[1];

 

$sql = 'ALTER TABLE `svar` ADD `  ' .$row1  .'` VARCHAR(320) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL';

$resultat = mysql_query($sql);

}

 

If I change the variable in the sql query to a $variable = "text string";it works, but not with $row1

 

Link to comment
Share on other sites

Yes, I can echo out $row1

 

If I use a different variable such as $variable = "text string"; I get this sql query:

ALTER TABLE `svar` ADD ` text string` VARCHAR(320) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL (which works)

 

If I use $row1 I get this sql query:

ALTER TABLE `svar` ADD ` the content of row1` VARCHAR(320) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL (which does not work)

Link to comment
Share on other sites

thanks for the help everybody, but I managed to figure it out. It was a syntax issue. here's the code that worked:

 

$sql2 = "ALTER TABLE `svar` ADD `$row1-` VARCHAR(320) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL";

$resultat = mysql_query($sql2);

 

for some reason I must put a character behind $row1, in order the get it to work,  this case I've used a "-", but it also works with other characters

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.