[英]Naming columns with alter table in MySQL using variables
Is it possible to name a column in a table using variables? 是否可以使用变量在表中命名列? For example lets say I want to create a table with columns named col1, col2, col3 using a while loop where the integer in the column name equals the current position in the loop.
例如,假设我要使用while循环创建带有名为col1,col2,col3的列的表,其中列名中的整数等于循环中的当前位置。 What should I replace the dots with below?
我该用下面的点替换什么?
ALTER TABLE table1 ADD ... INT(11) DEFAULT NULL;
$numberOfColumnsRequired = $_POST['user_requested_tables'];
$i = 1;
while ($i < $numberOfColumnsRequired && $i <= 30) {
$result = mysqli_query($link,"ALTER TABLE table1 ADD `col{$i}` INT(11) DEFAULT NULL");
$i++;
}
Something like this will accomplish the task... However this opens the door to a lot of potentially nasty things to happen. 这样的事情就可以完成任务...但是,这为发生许多潜在的令人讨厌的事情打开了大门。
Why do you need this? 你为什么需要这个? What are you trying to do?
你想做什么? Id rather propose a better solution.
我宁愿提出一个更好的解决方案。
Edit: added a maximum number of columns that can be added. 编辑:添加了可以添加的最大列数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.