Invoking a PHP script from a MySQL trigger

前端 未结 10 1892
时光取名叫无心
时光取名叫无心 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 07:59

    I was thinking about this exact issue for a case with long polling where I didn't want the php script to have to continually poll the db. Polling would need to be done somewhere, memory would probably be best. So if somehow the trigger could put the info into something like memcache, then php could poll that would would be much less intensive overall. Just need a method for mysql to use memcache. Perhaps into a predefined variable with a specific user id. Once the data is retrieved php could reset the var until the db sets it again. Not sure about timing issues though. Perhaps a second variable to store the previous key selected.

    0 讨论(0)
  • 2020-11-22 07:59

    I found this:

    http://forums.mysql.com/read.php?99,170973,257815#msg-257815

    DELIMITER $$
    CREATE TRIGGER tg1 AFTER INSERT ON `test`
    FOR EACH ROW
    BEGIN
    \! echo "php /foo.php" >> /tmp/yourlog.txt
    END $$
    DELIMITER ;
    
    0 讨论(0)
  • 2020-11-22 07:59

    Run away from store procedures as much as possible. They are pretty hard to maintain and are VERY OLD STUFF ;)

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

    If you have transaction logs in you MySQL, you can create a trigger for purpose of a log instance creation. 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.

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

    The trigger is executed on the MySQL server, not on the PHP one (even if those are both on the same machine).

    So, I would say this is not quite possible -- at least not simply.


    Still, considering this entry from the MySQL FAQ on Triggers :

    23.5.11: Can triggers call an external application through a UDF?

    Yes. For example, a trigger could invoke the sys_exec() UDF available here: https://github.com/mysqludf/lib_mysqludf_sys#readme

    So, there might be a way via an UDF function that would launch the php executable/script. Not that easy, but seems possible. ;-)

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

    That should be considered a very bad programming practice to call PHP code from a database trigger. If you will explain the task you are trying to solve using such "mad" tricks, we might provide a satisfying solution.

    ADDED 19.03.2014:

    I should have added some reasoning earlier, but only found time to do this now. Thanks to @cmc for an important remark. So, PHP triggers add the following complexities to your application:

    • Adds a certain degree of security problems to the application (external PHP script calls, permission setup, probably SELinux setup etc) as @Johan says.

    • Adds additional level of complexity to your application (to understand how database works you now need to know both SQL and PHP, not only SQL) and you will have to debug PHP also, not only SQL.

    • Adds additional point of failure to your application (PHP misconfiguration for example), which needs to be diagnosied also ( I think trigger needs to hold some debug code which will log somwewhere all insuccessful PHP interpreter calls and their reasons).

    • Adds additional point of performance analysis. Each PHP call is expensive, since you need to start interpreter, compile script to bytecode, execute it etc. So each query involving this trigger will execute slower. And sometimes it will be difficult to isolate query performance problems since EXPLAIN doesn't tell you anything about query being slower because of trigger routine performance. And I'm not sure how trigger time is dumped into slow query log.

    • Adds some problems to application testing. SQL can be tested pretty easily. But to test SQL + PHP triggers, you will have to apply some skill.

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