How to add new column to MYSQL table?

后端 未结 8 1685
南旧
南旧 2020-11-27 12:38

I am trying to add a new column to my MYSQL table using PHP. I am unsure how to alter my table so that the new column is created. In my assessment table I have:



        
相关标签:
8条回答
  • 2020-11-27 13:42
     $table  = 'your table name';
     $column = 'q6'
     $add = mysql_query("ALTER TABLE $table ADD $column VARCHAR( 255 ) NOT NULL");
    

    you can change VARCHAR( 255 ) NOT NULL into what ever datatype you want.

    0 讨论(0)
  • 2020-11-27 13:42

    Something like:

    $db = mysqli_connect("localhost", "user", "password", "database");
    $name = $db->mysqli_real_escape_string($name);
    $query = 'ALTER TABLE assesment ADD ' . $name . ' TINYINT NOT NULL DEFAULT \'0\'';
    if($db->query($query)) {
        echo "It worked";
    }
    

    Haven't tested it but should work.

    0 讨论(0)
提交回复
热议问题