使用 PDO 添加 MySQL 列(如果不存在)
我对 SQL 知之甚少,但找到了 SQL Server 的一个命令,可以将列添加到数据库(如果该列尚不存在)。遗憾的是,它在对我的 MySQL 数据库执行时不起作用,返回语法错误。
$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());
}
那么,如果“where int(2)”列不存在,我应该更改什么来创建它呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须创建一个存储过程来在一条语句中处理此问题。这是在这里讨论的: 将列添加到 mysql 表,如果它不存在
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