Invoking a PHP script from a MySQL trigger

前端 未结 10 1913
时光取名叫无心
时光取名叫无心 2020-11-22 07:31

Is there any way how to invoke a PHP page / function when a record is inserted to a MySQL database table? We don\'t have control over the record insertion procedure. Is ther

相关标签:
10条回答
  • 2020-11-22 08:11

    In order to get a notification from the database I wrote a command line script using websocket to check for the latest updated timestamp every second. This ran as an infinite loop on the server. If there is a change all connected clients will can be sent a notification.

    0 讨论(0)
  • 2020-11-22 08:22

    I don't know if it's possible but I always pictured myself being able to do this with the CSV storage engine in MySQL. I don't know the details of this engine: http://dev.mysql.com/doc/refman/5.7/en/csv-storage-engine.html but you can look into it and have a file watcher in your operating system that triggers a PHP call if the file is modified.

    0 讨论(0)
  • 2020-11-22 08:23

    A cronjob could monitor this log and based on events created by your trigger it could invoke a php script. That is if you absolutely have no control over you insertion.. If you have transaction logs in you MySQL, you can create a trigger for purpose of a log instance creation.

    0 讨论(0)
  • 2020-11-22 08:26

    A friend and I have figured out how to call Bernardo Damele's sys_eval UDF, but the solution isn't as elegant as I'd like. Here's what we did:

    1. Since we're using Windows, we had to compile the UDF library for Windows using Roland Bouman's instructions and install them on our MySQL server.
    2. We created a stored procedure that calls sys_eval.
    3. We created a trigger that calls the stored procedure.

    Stored Procedure code:

    DELIMITER $$
    CREATE PROCEDURE udfwrapper_sp
    (p1   DOUBLE,
     p2   DOUBLE,
     p3 BIGINT)
    BEGIN
     DECLARE cmd CHAR(255);
     DECLARE result CHAR(255);
     SET cmd = CONCAT('C:/xampp/php/php.exe -f "C:/xampp/htdocs/phpFile.php" ', p1, ' ', p2, ' ', p3);
     SET result = sys_eval(cmd);
    END$$;
    

    Trigger code:

    CREATE TRIGGER udfwrapper_trigger AFTER INSERT ON sometable
    FOR EACH ROW
    CALL udfwrapper_sp(NEW.Column1, NEW.Column2, NEW.Column3);
    

    I'm not thrilled about having the stored procedure, and I don't know if it creates extra overhead, but it does work. Each time a row is added to sometable, the trigger fires.

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