2

I am using a readymade script to backup my MySQL database using PHP. I store the resultant query in a variable.

If I echo the variable, and copy paste the output into the MySQL console, it works perfectly.

But when I run the same using 'mysql_query' (I know it is depreciated, kindly ignore that), I get the dreaded Syntax error.

Here's the echo output (first 2 lines) :

INSERT INTO assign VALUES('75085','rsam','CE0001/CZ0001/CPE183/CSC183','1','1','3.0','13','1','1','13','2','10.00','117.00','0','0');INSERT INTO assign VALUES('75086','rsam','CE0001/CZ0001/CPE183/CSC183','1','2','3.0','13','1','1','13','2','10.00','97.50','0','0');

And here's the exact 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 'INSERT INTO assign VALUES('75085','rsam','CE0001/CZ0001/CPE183/CSC183','1','1'' at line 1

If anyone can point out what I am obviously missing, I would be grateful!

Willem Van Onsem
  • 397,926
  • 29
  • 362
  • 485
Rohan Sood
  • 417
  • 1
  • 3
  • 17

2 Answers2

3

As the documentation for mysql_query() says:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

You might be interested in mysql_multi_query():

Executes one or multiple queries which are concatenated by a semicolon.

Amal Murali
  • 73,160
  • 18
  • 123
  • 143
Shakti Patel
  • 3,570
  • 4
  • 20
  • 29
2

While mysql_query is limited to a single statement, this situation can be avoided as multiple records can be inserted into the same table with only one statement:

INSERT INTO assign (...)
VALUES(...),
VALUES(...);

This will save on round-trip latency (over multiple mysql_query) which might matter.

See Inserting multiple rows in mysql

Community
  • 1
  • 1
user2864740
  • 57,407
  • 13
  • 129
  • 202
  • Thank you for the alternative, but unfortunately the list also has 'CREATE TABLE' queries, which I didn't include here for simplicity. – Rohan Sood Oct 12 '13 at 03:58