-2

I'm trying to get the following to run via a single pdo statement

UPDATE `coin_price` SET `coin_id` = 1 WHERE coin_id = 1;
UPDATE `coin_price` SET `coin_id` = 178 WHERE coin_id = 178;

and I am getting the following exception

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE `coin_price` SET `coin_id` = 178 WHERE coin_id = 178'` at line 1

I am just running

$statement = $this->system[$location]["connection"]->prepare($sql);
$statement->execute();

and getting an exception. I tried with binding and without.

Is what I am doing not possible?

Note : Both queries run fine via PDO by themselves

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
hendr1x
  • 1,470
  • 1
  • 14
  • 23

2 Answers2

2

Don't try to run multiple statements at once.

Run them separately. But, if you need transactional semantics, surround by START TRANSACTION and COMMIT. (Example: You don't want a crash between debiting one financial account and crediting another.)

Another plan is to write and CALL a Stored Procedure to combine the statements.

If you are concerned about performance, well, you are talking about very few milliseconds.

Rick James
  • 135,179
  • 13
  • 127
  • 222
1

You can only execute one query at a time. But you can combine them into a single query:

UPDATE coin_price
SET coin_id = 
    CASE coin_id
        WHEN 1 THEN 1
        WHEN 178 THEN 178
    END
WHERE coin_id IN (1, 178)
Barmar
  • 741,623
  • 53
  • 500
  • 612