kristian_gl Posted April 21, 2010 Share Posted April 21, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/ Share on other sites More sharing options...
swatisonee Posted April 21, 2010 Share Posted April 21, 2010 $result = mysql_query[$sql] ; Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1045673 Share on other sites More sharing options...
Mchl Posted April 21, 2010 Share Posted April 21, 2010 Does your MySQL user have privileges for ALTER statements? Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1045715 Share on other sites More sharing options...
kristian_gl Posted April 21, 2010 Author Share Posted April 21, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1045720 Share on other sites More sharing options...
oni-kun Posted April 21, 2010 Share Posted April 21, 2010 You claim you echo'd $row1 yet you're now saying it does not work? Also it is a very bad idea to have SQL statements within a loop, Use JOIN. Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1045722 Share on other sites More sharing options...
kristian_gl Posted April 21, 2010 Author Share Posted April 21, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1045729 Share on other sites More sharing options...
swatisonee Posted April 22, 2010 Share Posted April 22, 2010 there is a space after the number 1 and before the dot .$row1 . Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1046500 Share on other sites More sharing options...
kristian_gl Posted April 22, 2010 Author Share Posted April 22, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1046578 Share on other sites More sharing options...
Mchl Posted April 22, 2010 Share Posted April 22, 2010 Perhaps there already was a column with this identifier in the table? Quote Link to comment https://forums.phpfreaks.com/topic/199237-add-a-new-column-to-mysql-table/#findComment-1046601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.