2

I want to issue multiple mysql commands with one mysql_query function. This is my code:

$query .= "INSERT INTO `users` VALUES(1,'stack','overflow');";
$query .= "INSERT INTO `posts` VALUES('other','stack','overflow');";
mysql_query($query);

If I do that I get a warning that my syntax would be incorrect. If I echo the output, copy it and execute it in phpMyAdmin it works.

Where is the error there?

Matthias Dunnowa
  • 119
  • 1
  • 1
  • 12
  • 1
    http://stackoverflow.com/questions/802437/how-to-execute-two-mysql-queries-as-one-in-php-mysql Perhaps duplicate? – Rey Gonzales Jun 19 '12 at 18:06
  • 1
    See also [the documentation for `mysql_query`](http://php.net/manual/en/function.mysql-query.php): "sends a unique query (multiple queries are not supported)" – sczizzo Jun 19 '12 at 18:08

3 Answers3

4
$query = "INSERT INTO `users` VALUES (1,'stack','overflow'), ('other','stack','overflow');";
mysql_query($query);

PHP does not support sending more than one query at a time via mysql_query, but you can achieve your result in a single one by using the above.

Jay
  • 3,285
  • 1
  • 20
  • 19
2

according to http://www.php.net/manual/en/function.mysql-query 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.

but this guy said that you just have to pass flag 65536 as mysql_connect's 5 parameter http://www.php.net/manual/en/function.mysql-query.php#91669

Fivell
  • 11,829
  • 3
  • 61
  • 99
2

I think you need this?? http://us2.php.net/manual/en/mysqli.multi-query.php

Don
  • 863
  • 1
  • 8
  • 22