开发者

Add MySQL column if not existing using PDO

开发者 https://www.devze.com 2023-02-20 16:11 出处:网络
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 synt

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号