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->开发者_StackOverflow社区;errorInfo());
}
So what should I change to create the column 'where int(2)' if it doesn't exist?
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
精彩评论