sql >> Databasteknik >  >> RDS >> Mysql

Skapa mysql trigger via PHP?

Medan mysqli gör inget med DELIMITER i multi-query uttalanden, det gör faktiskt ingenting med några avgränsare alls i vanliga frågor, så bara tryck in dina triggers en efter en:

$ 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: [email protected]
character_set_client: latin1
collation_connection: latin1_swedish_ci
  Database Collation: utf8_general_ci
1 row in set (0.00 sec)



  1. Få MySQL-databasutdata via PHP till XML

  2. MySQL-jämförelse med nullvärde

  3. Problem med Postgres ALTER TABLE

  4. Optimera MySQL för ALTER TABLE av InnoDB