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
https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/
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

 

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)

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

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.