tags:

views:

65

answers:

2

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?

For example, on query match Fire start email:

To: [email protected]
From: [email protected]
Subject: Set query met, auto email complete.

It would be around MySQL5. I'm stuck on this. Am I best to do it in Perl or similar? Perhaps it could also email various users? (This isn't the hard bit. :P)

A: 

You can use PHP or Perl or any other programming language you like.

Create a cronjob to execute your script every X hours.

For example php queryDatabase.php.

That's it.

Ghommey
I'm aware of CRON, very useful tool :-)What about the content in the actual script? Can someone just give me an idea on where I start?Never done an email/trigger script before. Only done MySQL queries :-)
Dean
Sending emails with php http://php.net/manual/en/function.mail.php
Ghommey
+2  A: 

Using cron or similar scheduler involves polling, which works but is brute force and ignorance. I can't recommend it: there's a time delay between when the row changes and when cron executes the checking program; 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?. 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.

daxim
Sweet, thanks daxim. Exact answer I wanted, shall try it out in a few hours :D
Dean