Is this legal?
这合法吗?
$string1= "INSERT INTO....;";
$string1 .= "INSERT INTO....;";
$string1 .= "INSERT INTO....;";
mysql_query($string1) or die(mysql_error());
7 个解决方案
#1
44
For what it's worth, and depending on if you're inserting the same data into the same tables, it's much better to insert multiple values with the one insert e.g.
对于它的价值,并且取决于您是否将相同的数据插入到相同的表中,最好使用一个插入插入多个值,例如
INSERT INTO a VALUES (1,23),(2,34),(4,33);
INSERT INTO a VALUES (8,26),(6,29);
#2
34
No, mysql_query()
only allows one query at a time.
不,mysql_query()一次只允许一个查询。
You can insert multiple rows like this:
您可以像这样插入多行:
INSERT INTO table (col1, col2)
VALUES (1, 2), (3, 4), (5, 6)
#3
10
From MySQL dev support MySQL dev forum
从MySQL开发支持MySQL开发论坛
INSERT INTO table (artist, album, track, length)
VALUES
("$artist", "$album", "$track1", "$length1"),
("$artist", "$album", "$track2", "$length2"),
("$artist", "$album", "$track3", "$length3"),
("$artist", "$album", "$track4", "$length4"),
("$artist", "$album", "$track5", "$length5");
So insert goes as normal as always:
所以插入像往常一样正常:
- naming first the table name where we want to insert new row,
- 首先命名我们要插入新行的表名,
- followed by naming column names in round brackets (Note: Not needed if you want to insert ALL columns),
- 然后在圆括号中命名列名(注意:如果要插入所有列,则不需要),
- followed by VALUES key name and then in round brackets comes the values that you want to insert for new ROW in the above table,
- 接下来是VALUES键名,然后在圆括号中出现你想在上表中为新ROW插入的值,
- followed by COMMA and then another pair of round brackets with new values for new row in the mentioned table above
- 然后是COMMA,然后是另一对圆括号,上面提到的表格中有新行的新值
- and this repeats N-times as long you have the data to insert.
- 只要你有数据要插入,这就会重复N次。
Happy inserting multiple values with ONE insert statement. :)
快乐插入多个值与一个插入语句。 :)
#4
3
Copy/paste example within a function and a loop (suppose $ids is an array)
在函数和循环中复制/粘贴示例(假设$ ids是一个数组)
public function duplicateItem($ids)
{
if (isset($ids[0])){ //at least one item
$sqlQuery = "INSERT INTO items (item_id, content) VALUES ";
for($i=0; $i<count($ids); $i++) {
if ($i == count($ids)-1){
$sqlQuery .= "(".$ids[$i][0].", '".$ids[$i][1]."');";
}else{
$sqlQuery .= "(".$ids[$i][0].", '".$ids[$i][1]."'),";
}
}
mysql_query($sqlQuery) or die('Error, insert query failed: '.mysql_error());
}
}
#5
2
In general, that's valid SQL since each statement ends with a semicolon, but PHP doesn't allow you to send more than one query at a time, in order to protect against SQL injection attacks that might exploit a poorly written script.
一般来说,这是有效的SQL,因为每个语句都以分号结尾,但PHP不允许您一次发送多个查询,以防止可能利用写得不好的脚本的SQL注入攻击。
You can still use a syntax like:
你仍然可以使用如下语法:
INSERT INTO foo VALUES ('a1', 'b1'), ('a2', 'b2');
#6
0
No. They are separate queries, and must be called as such.
不。它们是单独的查询,必须这样调用。
#7
-1
INSERT INTO table (a,b) VALUES (1,2), (2,3), (3,4);
#1
44
For what it's worth, and depending on if you're inserting the same data into the same tables, it's much better to insert multiple values with the one insert e.g.
对于它的价值,并且取决于您是否将相同的数据插入到相同的表中,最好使用一个插入插入多个值,例如
INSERT INTO a VALUES (1,23),(2,34),(4,33);
INSERT INTO a VALUES (8,26),(6,29);
#2
34
No, mysql_query()
only allows one query at a time.
不,mysql_query()一次只允许一个查询。
You can insert multiple rows like this:
您可以像这样插入多行:
INSERT INTO table (col1, col2)
VALUES (1, 2), (3, 4), (5, 6)
#3
10
From MySQL dev support MySQL dev forum
从MySQL开发支持MySQL开发论坛
INSERT INTO table (artist, album, track, length)
VALUES
("$artist", "$album", "$track1", "$length1"),
("$artist", "$album", "$track2", "$length2"),
("$artist", "$album", "$track3", "$length3"),
("$artist", "$album", "$track4", "$length4"),
("$artist", "$album", "$track5", "$length5");
So insert goes as normal as always:
所以插入像往常一样正常:
- naming first the table name where we want to insert new row,
- 首先命名我们要插入新行的表名,
- followed by naming column names in round brackets (Note: Not needed if you want to insert ALL columns),
- 然后在圆括号中命名列名(注意:如果要插入所有列,则不需要),
- followed by VALUES key name and then in round brackets comes the values that you want to insert for new ROW in the above table,
- 接下来是VALUES键名,然后在圆括号中出现你想在上表中为新ROW插入的值,
- followed by COMMA and then another pair of round brackets with new values for new row in the mentioned table above
- 然后是COMMA,然后是另一对圆括号,上面提到的表格中有新行的新值
- and this repeats N-times as long you have the data to insert.
- 只要你有数据要插入,这就会重复N次。
Happy inserting multiple values with ONE insert statement. :)
快乐插入多个值与一个插入语句。 :)
#4
3
Copy/paste example within a function and a loop (suppose $ids is an array)
在函数和循环中复制/粘贴示例(假设$ ids是一个数组)
public function duplicateItem($ids)
{
if (isset($ids[0])){ //at least one item
$sqlQuery = "INSERT INTO items (item_id, content) VALUES ";
for($i=0; $i<count($ids); $i++) {
if ($i == count($ids)-1){
$sqlQuery .= "(".$ids[$i][0].", '".$ids[$i][1]."');";
}else{
$sqlQuery .= "(".$ids[$i][0].", '".$ids[$i][1]."'),";
}
}
mysql_query($sqlQuery) or die('Error, insert query failed: '.mysql_error());
}
}
#5
2
In general, that's valid SQL since each statement ends with a semicolon, but PHP doesn't allow you to send more than one query at a time, in order to protect against SQL injection attacks that might exploit a poorly written script.
一般来说,这是有效的SQL,因为每个语句都以分号结尾,但PHP不允许您一次发送多个查询,以防止可能利用写得不好的脚本的SQL注入攻击。
You can still use a syntax like:
你仍然可以使用如下语法:
INSERT INTO foo VALUES ('a1', 'b1'), ('a2', 'b2');
#6
0
No. They are separate queries, and must be called as such.
不。它们是单独的查询,必须这样调用。
#7
-1
INSERT INTO table (a,b) VALUES (1,2), (2,3), (3,4);