Page 1 of 1

Multiple INSERTs in mysql_query()

Posted: Tue Aug 10, 2004 1:45 pm
by anjanesh
I want to insert more than one row into a table. I can do the for loop but Im trying to avoid querying many times. But Im not able to get this to work:

Code: Select all

mysql_select_db("test");
$sql="
INSERT INTO `test1` VALUES ('q','w','e','5');
INSERT INTO `test1` VALUES ('q','w','e','6');
INSERT INTO `test1` VALUES ('q','w','e','7');
INSERT INTO `test1` VALUES ('q','w','e','8');
";
$res=mysql_query($sql);
if (!$res) echo $sql.'<br>'.mysql_error();
Any idea what to do ? I tried adding \r\n after the semi colon but no luck.
Thanks

Posted: Tue Aug 10, 2004 2:07 pm
by feyd

Code: Select all

INSERT INTO `test1`
VALUES('q','w','e','5'),
VALUES('q','w','e','6'),
VALUES('q','w','e','7'),
VALUES('q','w','e','8'),
VALUES('q','w','e','9')

Posted: Tue Aug 10, 2004 2:10 pm
by anjanesh
Oh. So we can execute multiple queries at a time ? Like if $sql were :
"
SELECT * FROM table1
DELETE FROM table2
";

Posted: Tue Aug 10, 2004 2:21 pm
by feyd
I don't think that would work.. it definitely won't work without semicolons to seperate the commands.. but I do know that mysql at least allows multiple inserts like how I wrote it.

Posted: Fri Aug 13, 2004 6:54 pm
by Serberus
You can compound multiple inserts together as shown above, you can't issue a SELECT and a DELETE in the same statement though, what would be passed back to PHP? How do you know which part of you query failed (or succeeded)? You want the result set from the SELECT and a TRUE/FALSE from the DELETE.