April 9, 2012
How to use current date variable to add new column in table?
Question by Abdullah Adam
i want to alter table add column with the current date and hour like below its not adding column but when i remove variable and make it simple text its add and please also inform me how to make this column name unique so that we not able to add same column again in mysql again
//get current date
$cname = date("Ymdh");
echo "v1" . $cname ;
//add curent date column if yet not added
mysql_query("ALTER TABLE groups_ids add " . $cname . "VARCHAR(35)") ;
Answer by Starx
The problem is in the query you wrote. First you have escape the column name using a backtick (`), after that there is no space before the datatype
mysql_query("ALTER TABLE groups_ids add `" . $cname . "` VARCHAR(35)") ;
//^ No space here