【发布时间】:2014-02-09 15:36:36
【问题描述】:
我有一个名为 $columns 的数组:
array(3) {
[0]=>
string(8) "Food"
[1]=>
string(6) "Calories"
[2]=>
string(3) "Carbs"
}
array(3) {
[0]=>
string(8) "Food"
[1]=>
string(6) "Calories"
}
另一个叫值
array(5) {
[0]=>
string(4) "'Fish'"
[1]=>
string(7) "'100'"
[2]=>
string(13) "'0'"
}
array(6) {
[0]=>
string(4) "'Tatoe'"
[1]=>
string(7) "'100'"
}
我的数据库中有代表数组所有可能列的列。根据项目的不同,数组可能有更多或更少的列。
我有这段代码假设在这个意义上生成 sql 语句:
INSERT INTO MEALS(该数组中的列)VALUES(值数组中的值)
代码如下:
for($i = 0; $i < count($column); $i++)
{
echo("INSERT INTO 'MEALS` ($column[$i]) VALUES ($values[$i])\n");
}
问题在于每个“MEAL”:
将生成n(每列)个sql查询。
这样:
INSERT INTO `MEALS` (Food) VALUES ('Fish')
INSERT INTO `MEALS` (Calory) VALUES ('100')
INSERT INTO `MEALS` (Carbs) VALUES ('0')
而不是全部都在同一个查询中。
插入MEALS(食物、卡路里、碳水化合物)值('Fish'、'100'、'0')
所以如果我在数据库中插入,将创建 3 行。
我知道我缺少逻辑,感谢任何帮助
【问题讨论】:
-
为什么不将插入连接到一个?
INSERT INTO MEALS (Your,Column,Names) VALUES(Your,Values,here),(Another,Value,there);?
标签: php