4

I'm trying to do something like this:

mysql_query("
  UPDATE name SET money = money + 1;
  UPDATE surname SET money = money + 1;
"); 

but it doesn't work.

It's just example, but my question is: How can I put two or even more queries in one mysql_query?

ax.
  • 56,201
  • 7
  • 77
  • 69
good_evening
  • 20,557
  • 64
  • 184
  • 294

4 Answers4

12

http://docs.php.net/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 .

But you might be interested in mysqli::multi_query:

Executes one or multiple queries which are concatenated by a semicolon.
VolkerK
  • 93,904
  • 19
  • 160
  • 225
5

You should use transactions for queries that need to happen in an atomic fashion, which I suspect these may.

Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
0

This can be done using the MySQLi interface, mysqli_multi_query() in particular. http://ca3.php.net/manual/en/mysqli.multi-query.php

It should be noted that you need to be extra careful with your escaping when using this function as any SQL injection attack has a much broader possible affect.

preinheimer
  • 3,680
  • 19
  • 34
-3

or perhaps you could try this...

$query1 ="UPDATE name SET money = money + 1;";
$query2 ="UPDATE surname SET money = money + 1";

mysql_query($query1,$query2) or die(mysql_error());