gpt4 book ai didi

php - SQL添加列名关键字

转载 作者:行者123 更新时间:2023-11-29 22:56:01 26 4
gpt4 key购买 nike

我尝试使用 PHP 中的 for 循环将列添加到表中,其中每个列名称都是数组中的值

for($i=0; $i<count($column); $i++){
query("ALTER TABLE table1 ADD COLUMN ".$column[$i]." int(3) DEFAULT 0");
}

它适用于所有列名,但是当涉及到列名“For”时,我收到此错误。

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax near 'For int(3) DEFAULT 0'

我假设错误是因为列名是关键字,因为数组 $columns 中的所有其他值都工作正常。

对于如何在表中添加列名“For”或任何其他 SQL 关键字有什么建议吗?如有任何反馈,我们将不胜感激。提前致谢。

最佳答案

由@GordonLinoff 回答

更改了查询,在列名称周围添加反引号

query("ALTER TABLE table1 ADD COLUMN `".$column[$i]."` int(3) DEFAULT 0");

关于php - SQL添加列名关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com