96

I have this, but it doesn't work:

$query = "ALTER TABLE `".$table_prefix."posts_to_bookmark` 
            ADD `ping_status` INT( 1 ) NOT NULL BEFORE `onlywire_status`";

I appreciate it!

0

1 Answer 1

218
$query = "ALTER TABLE `" . $table_prefix . "posts_to_bookmark` 
          ADD COLUMN `ping_status` INT(1) NOT NULL 
          AFTER `<TABLE COLUMN BEFORE THIS COLUMN>`";

I believe you need to have ADD COLUMN and use AFTER, not BEFORE.

In case you want to place column at the beginning of a table, use the FIRST statement:

$query = "ALTER TABLE `" . $table_prefix . "posts_to_bookmark`
          ADD COLUMN `ping_status` INT(1) NOT NULL 
          FIRST";

http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

3
  • 42
    Yep, AFTER or FIRST, not BEFORE. +1
    – dwich
    Jul 31, 2010 at 19:16
  • 1
    For use BEFORE has sense to understand "why I want put this field there". When I target the first place of a "serie" of fields in my row, I just add " # AKA BEFORE fieldname
    – Moosh
    Jul 12, 2017 at 9:50
  • use [FIRST | AFTER col_name] dev.mysql.com/doc/refman/8.0/en/alter-table.html
    – caot
    Nov 1 at 21:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.