Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,940
38
Australia
Hi,

Right now when I do I query its like this:

PHP:
$sql = "INSERT INTO someTable (rowName) VALUES ('someText')";

Can I do this:

PHP:
$sql = "INSERT INTO someTable (rowName) VALUES ('someText'); INSERT INTO anotherTable (rowName) VALUES ('someText')";
mysql_query($sql, $con);
 

DJBenE

macrumors member
Jul 9, 2010
60
0
Rowland Heights, California
Hi,

Right now when I do I query its like this:

PHP:
$sql = "INSERT INTO someTable (rowName) VALUES ('someText')";

Can I do this:

PHP:
$sql = "INSERT INTO someTable (rowName) VALUES ('someText'); INSERT INTO anotherTable (rowName) VALUES ('someText')";
mysql_query($sql, $con);

In short...yes.

But, if you're inserting multiple rows into the same table:
PHP:
$sql = "INSERT INTO someTable (rowName) VALUES ('someText'), ('someMoreText'), ('someMoreText');";

All together:
PHP:
$sql = "INSERT INTO someTable (rowName) VALUES ('someText'), ('someMoreText'), ('someMoreText');
INSERT INTO anotherTable (rowName) VALUES ('someText');
INSERT INTO yetAnotherTable (rowName) VALUES ('someText'), ('someText');";
mysql_query($sql,$con);
 

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,940
38
Australia
Hi again,

I'm basically doing:
PHP:
sql = "INSERT INTO table1 (row1) VALUES ('SomeValue'); INSERT INTO table2 (row2) VALUES ('SomeValue');"; 
if (!mysql_query($sql, $con)){
    die(mysql_error());
}

And, all I get is an error that says:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; INSERT INTO table2 (row2) VALUES ('0')' at line 1

Any idea whats wrong?
 

jSunbeam

macrumors regular
Nov 9, 2007
115
0
Hi again,

I'm basically doing:
PHP:
sql = "INSERT INTO table1 (row1) VALUES ('SomeValue'); INSERT INTO table2 (row2) VALUES ('SomeValue');"; 
if (!mysql_query($sql, $con)){
    die(mysql_error());
}

And, all I get is an error that says:


Any idea whats wrong?

Yeah - you can't execute multiple SQL statements from PHP in the way that you're doing it (semi-colon separated), you just can't.
Have a look into using PDO for your database transactions: http://php.net/manual/en/book.pdo.php
 

microcolt

macrumors regular
Apr 23, 2010
128
1
Behind You
Whenever you do MySQL queries, try them in phpMyAdmin if possible and split everything up in lines until you zero-in on the problem.
 

DJBenE

macrumors member
Jul 9, 2010
60
0
Rowland Heights, California
Hi again,

I'm basically doing:
PHP:
sql = "INSERT INTO table1 (row1) VALUES ('SomeValue'); INSERT INTO table2 (row2) VALUES ('SomeValue');"; 
if (!mysql_query($sql, $con)){
    die(mysql_error());
}

And, all I get is an error that says:


Any idea whats wrong?

Sorry, I was mistaken when showing you how to execute multiple queries using mysql_query, as I've been using mysqli for the past few years and have gotten used to it.
Using mysqli's multi-query you can accomplish what you need, although you'll have to use mysqli instead of mysql.
 

MythicFrost

macrumors 68040
Original poster
Mar 11, 2009
3,940
38
Australia
Sorry, I was mistaken when showing you how to execute multiple queries using mysql_query, as I've been using mysqli for the past few years and have gotten used to it.
Using mysqli's multi-query you can accomplish what you need, although you'll have to use mysqli instead of mysql.
Oh ok, thanks. It's not a big deal, I've found a way to do it.
 

jayanti

macrumors newbie
Jun 17, 2016
1
0
Yeah - you can't execute multiple SQL statements from PHP in the way that you're doing it (semi-colon separated), you just can't.
Have a look into using PDO for your database transactions: http://php.net/manual/en/book.pdo.php



i am getting same error
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
how can i remove it and i didnt understand what you want to say about this link
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.