简体   繁体   English

MySQL查询为真后,如何挂接到触发的事件?

[英]How do I hook into an event triggered once a MySQL query is true?

How does one create a PHP script that checks the MySQL row consistently and if a set query is matched, it starts an event, such as sending an email? 如何创建一个PHP脚本来一致地检查MySQL行,以及是否匹配设置的查询,它会启动一个事件,例如发送电子邮件?

For example, on query match Fire start email: 例如,在查询中匹配Fire启动电子邮件:

To: me@me.com
From: php@trigger.com
Subject: Set query met, auto email complete.

It would be around MySQL5. 它将围绕MySQL5。 I'm stuck on this. 我一直坚持下去。 Am I best to do it in Perl or similar? 我最好在Perl或类似版本中做到吗? Perhaps it could also email various users? 也许它还可以通过电子邮件向各种用户发送电子邮件? (This isn't the hard bit. :P) (这不是难点。:P)

Using cron or similar scheduler involves polling , which works but is brute force and ignorance . 使用cron或类似的调度程序涉及轮询 ,虽然有效,但蛮力和无知 I can't recommend it: there's a time delay between when the row changes and when cron executes the checking program; 我不建议这样做:行更改与cron执行检查程序之间会有一段时间的延迟; repeated checking is a waste of resources. 反复检查是浪费资源。

The elegant solution is database trigger, see Using Triggers and Can triggers call an external application through a UDF? 优雅的解决方案是数据库触发器,请参阅使用触发器触发器 可以通过UDF调用外部应用程序吗? . You will get an event whenever your table row is on fire or something, and your email sending program is called immediately. 每当表行着火或发生任何事情时,您都会收到一个事件,并且您的电子邮件发送程序将立即被调用。

If you want to use Perl, Email::Simple / Email::Sender is nifty. 如果要使用Perl, Email::Simple / Email::Sender很漂亮。

You can use PHP or Perl or any other programming language you like. 您可以使用PHP或Perl或您喜欢的任何其他编程语言。

Create a cronjob to execute your script every X hours. 创建一个cronjob以每X小时执行一次脚本。

For example php queryDatabase.php . 例如php queryDatabase.php

That's it. 而已。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM