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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!