shanethehat
shanethehat

Reputation: 15570

Add MySQL column if not existing using PDO

I know very little SQL, but found a command for SQL Server to add a column to the database if it doesn't already exist. Sadly it doesn't work when executed against my MySQL database, returning a syntax error.

$query = $dbh->prepare("if not exists (select * from syscolumns where id=object_id(':table_name') and name='where') alter table :table_name add where int(2)");

if($query->execute(array(':table_name'=>'registrations'))) {
    //twist and shout
} else {
    print_r($query->errorInfo());   
}

So what should I change to create the column 'where int(2)' if it doesn't exist?

Upvotes: 1

Views: 2786

Answers (1)

sreimer
sreimer

Reputation: 4998

You would have to create a stored procedure to handle this within one statement. This was discussed here: add column to mysql table if it does not exist

Upvotes: 1

Related Questions