Create mysql trigger via PHP?

前端 未结 3 1013
挽巷
挽巷 2020-12-03 18:52

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`          


        
相关标签:
3条回答
  • 2020-12-03 19:09

    this might help as well: the following creates a trigger on selectes tables. change the code and it might do what you want. https://github.com/junicom/mysqltriggerscript

    0 讨论(0)
  • 2020-12-03 19:20

    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)
    
    0 讨论(0)
  • 2020-12-03 19:26

    This may help you:

    PHP: multiple SQL queries in one mysql_query statement

    0 讨论(0)
提交回复
热议问题