good_evening
good_evening

Reputation: 21749

How can I put two queries in one mysql_query?

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?

Upvotes: 4

Views: 8723

Answers (4)

Archangel08
Archangel08

Reputation: 91

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());

Upvotes: -3

preinheimer
preinheimer

Reputation: 3722

This can be done using the MySQLi interface, mysqli_multi_query() in particular. https://www.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.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798626

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

Upvotes: 5

VolkerK
VolkerK

Reputation: 96159

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.

Upvotes: 12

Related Questions