M
M
Mechanic2020-04-22 00:34:54
PHP
Mechanic, 2020-04-22 00:34:54

Adding a mysql trigger from PHP (with delimiter change) possible?

Good afternoon friends! I need to interact with mysql triggers (add/remove) using PHP. Please, prompt by what means it is realizable. Error 1064 (syntax), but it's understandable - there can be only one muscle command in a request from PHP. How to bypass it? Only .. exec() remained in my head?
I would be very grateful. Thank you!

DELIMITER //
CREATE TRIGGER before_update_elem BEFORE UPDATE ON dc_operations FOR EACH ROW BEGIN
   CASE
      WHEN (...) THEN
         INSERT INTO ...

      WHEN (...) THEN
         INSERT INTO ...
      ELSE BEGIN END;
   END CASE;
END//
DELIMITER ;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kstkn, 2020-04-25
@kstkn

Just don't use DELIMITER:

<?php

$d = new \PDO('mysql:host=mysql;dbname=test', 'username', 'password');
$sql = <<<SQL
CREATE TRIGGER before_update_elem BEFORE UPDATE ON dc_operations FOR EACH ROW BEGIN
   CASE
      WHEN (...) THEN
         INSERT INTO ...

      WHEN (...) THEN
         INSERT INTO ...
      ELSE BEGIN END;
   END CASE;
END
SQL;
$d->exec($sql);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question