Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create mysql trigger via PHP?

I'm executing the following in PHP5.3:

$sql = "
CREATE TRIGGER `_photo_fulltext_insert` AFTER INSERT ON `photo` FOR EACH ROW INSERT INTO `_photo_fulltext` SELECT * FROM `photo` WHERE `id`=NEW.`id`;
CREATE TRIGGER `_photo_fulltext_delete` AFTER DELETE ON `photo` FOR EACH ROW DELETE FROM `_photo_fulltext` WHERE `id`=OLD.`id`;

DELIMITER |
CREATE TRIGGER `_photo_fulltext_update` AFTER UPDATE ON `photo`
  FOR EACH ROW BEGIN
    DELETE FROM `_photo_fulltext` WHERE `id`=NEW.`id`;
    INSERT INTO `_photo_fulltext` SELECT * FROM `photo` WHERE `id`=NEW.`id`;
  END;
|
DELIMITER ;
";

$mysqli->multi_query($sql);

The "photo_fulltext_update" trigger isn't getting created. This statement does run (and creates all the triggers) in phpMyAdmin. I've read online somewhere that the MySQL server doesn't support the DELIMITER statement at all, so I'm looking for a way to re-write this multi-step CREATE TRIGGER statement so that mysqli::multi_query can send it to MySQL.

like image 296
brianjcohen Avatar asked Sep 21 '10 17:09

brianjcohen


People also ask

What is PHP trigger function?

PHP trigger_error() Function The trigger_error() function creates a user-level error message. The trigger_error() function can be used with the built-in error handler, or with a user-defined function set by the set_error_handler() function.

How do I create a trigger in MySQL workbench?

Let's see the steps to create a BEFORE INSERT Trigger using MySQL Workbench. #1) Right-Click on the table name where the trigger needs to be created. #4) Click on the '+' sign adjacent to the BEFORE INSERT section to create a trigger. #6) Click “Apply” to Save the Trigger.


1 Answers

While the mysqli doesn't to anything with DELIMITER in multi-query statements, it actually doesn't do anything with any delimiters at all in normal queries, so just shove your triggers in one by one:

$ cat i.php 
<?php
$mysqli = new mysqli('localhost', 'test', '', 'test');
$sql = "
CREATE TRIGGER `_foo_fulltext_update` AFTER UPDATE ON `foo`
  FOR EACH ROW BEGIN
    DELETE FROM `bar` WHERE `bar`=NEW.`bar`;
    INSERT INTO `bar` (bar) SELECT bar FROM `foo` WHERE `bar`=NEW.`bar`;
  END;
";
$mysqli->query($sql);
var_dump($mysqli->error);
$ php i.php 
string(0) ""
$ mysql
mysql> use test;

Database changed
mysql> show triggers\G
*************************** 1. row ***************************
             Trigger: _foo_fulltext_update
               Event: UPDATE
               Table: foo
           Statement: BEGIN
    DELETE FROM `bar` WHERE `bar`=NEW.`bar`;
    INSERT INTO `bar` (bar) SELECT bar FROM `foo` WHERE `bar`=NEW.`bar`;
  END
              Timing: AFTER
             Created: NULL
            sql_mode: 
             Definer: root@localhost
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)
like image 81
Wrikken Avatar answered Sep 30 '22 00:09

Wrikken