tags:

views:

383

answers:

4

Is this legal?

$string1= "INSERT INTO....;";
$string1 .= "INSERT INTO....;";
$string1 .= "INSERT INTO....;";
mysql_query($string1) or die(mysql_error());
A: 

No. They are separate queries, and must be called as such.

Josh Leitzel
+2  A: 

No, mysql_query() only allows one query at a time.

You can insert multiple rows like this:

INSERT INTO table (col1, col2)
VALUES (1, 2), (3, 4), (5, 6)
Greg
+2  A: 

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);
Gavin Gilmour
A: 

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.

You can still use a syntax like:

INSERT INTO foo VALUES ('a1', 'b1'), ('a2', 'b2');
VoteyDisciple