0

I want to add some columns in to existing database table after the last column i want to add these.. the scritp will be avalaible for everyone so not sure what will be the last column.

$DB_Column = array (
        'About_Me' => "About", 
        'Avatar' => "Avatar",
        'Clan_Tag' => "Clan",
        'Month' => "Month",
        'Day' => "Day",
        'Year' => "Year",
        'Website' => "Website",
        'Website_Link' => "Link",
        'Website_Link1' => "Link1",
        'Email_Address' => "Email"
    );

$Database->exec('ALTER TABLE '.$DB_Table.' 

            ADD COLUMN '.$DB_Column['About_Me'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Avatar'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Clan_Tag'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Month'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Day'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Year'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Website'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Website_Link'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Website_Link1'].' VARCHAR(255),
            ADD COLUMN '.$DB_Column['Email_Address'].' VARCHAR(255) 
    ');

Getting this error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1 near ",": syntax error' in C:\xampp\htdocs\Webstats\Functions.php:30 Stack trace: #0 C:\xampp\htdocs\Webstats\Functions.php(30): PDO->exec('ALTER TABLE RCO...') #1 C:\xampp\htdocs\Webstats\Index.php(3): require('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\Webstats\Functions.php on line 30

2 Answers2

1

The ALTER TABLE statement allows only one column.

CL.
  • 173,858
  • 17
  • 217
  • 259
0

You cannot add more than one column using alter table command.

enter image description here

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Sir check this http://stackoverflow.com/questions/17541312/alter-table-add-multiple-columns-after-column1 –  Aug 20 '14 at 19:15
  • @JavedAhmadzai:- Did you checked that it is a MYSQL not SQLITE! In MySql it is allowed but not in Sqlite:) – Rahul Tripathi Aug 20 '14 at 19:16
  • @JavedAhmadzai:- No it is not possible in Sqlite! :( you need to have repeated calls to ALTER TABLE. – Rahul Tripathi Aug 20 '14 at 19:18
  • Well, repeating alter table is not bad but what about AFTER i want automatic last column to be selected and new column to be added after last one! can it be>? –  Aug 20 '14 at 19:23
  • @JavedAhmadzai:- I am not sure what exactly you are trying to achieve. I would suggest you may better ask a new question if that is a different issue :) – Rahul Tripathi Aug 20 '14 at 19:25
  • Still getting error is this syntax correct? $Database->exec('ALTER TABLE '.$DB_Table.' ADD COLUMN '.$DB_Column['About_Me'].' VARCHAR(255)'); $Database->exec('ALTER TABLE '.$DB_Table.' ADD COLUMN '.$DB_Column['Avatar'].' VARCHAR(255)'); –  Aug 20 '14 at 19:40
  • @JavedAhmadzai:- Pass only one statement inside exec at a time :) – Rahul Tripathi Aug 20 '14 at 19:41
  • Passing one but not working see $Database->exec('ALTER TABLE '.$DB_Table.' ADD '.$DB_Column['About_Me'].' VARCHAR(255)'); –  Aug 20 '14 at 19:42
  • @JavedAhmadzai:- What is the error which you are getting? – Rahul Tripathi Aug 20 '14 at 19:46
  • @JavedAhmadzai:- Try this:- `$Database->exec('ALTER TABLE '+.$DB_Table.+' ADD '+.$DB_Column['About_Me']. '+ VARCHAR(255)')` – Rahul Tripathi Aug 20 '14 at 19:50
  • Syntax error '+.$DB_Column['About_Me'].'+ aslo tried .+' but still –  Aug 20 '14 at 19:54
  • @JavedAhmadzai:- That is because of the space you need after the .$DB_Column['About_Me']. Please try this:- `$Database->exec('ALTER TABLE '+.$DB_Table.+' ADD '+.$DB_Column['About_Me']. '+ VARCHAR(255)') ` – Rahul Tripathi Aug 20 '14 at 19:55