As the title says, the use of crons is the most widely use because mainly the limitations of the offered DB solution by the hosting companies (MySQL).
What if you could create jobs from inside the DB itself to make the work that usually you would be doing by the use of crons or even timestamped events?
Well as of MySQL 5.1 you can do it. Jobs on MySQL are simply called events, and they work EXACTLY the same way as your crons.
I will ilustrate with a small example, but remember that only MySQL 5.1 or higher is capable to do this.
Code: Select all
CREATE TABLE IF NOT EXISTS `table` (
`tID` int(11) NOT NULL AUTO_INCREMENT,
`tDATETIME` datetime NOT NULL,
UNIQUE KEY `tID` (`tID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Code: Select all
CREATE EVENT gettime
ON SCHEDULE EVERY 1 MINUTE DO
INSERT INTO test.table values ('',current_timestamp)
Code: Select all
set global event_scheduler = 1;
And 9 minutes later
Code: Select all
SELECT * FROM test.table
1 2014-01-08 18:12:17
2 2014-01-08 18:13:17
3 2014-01-08 18:14:17
4 2014-01-08 18:15:17
5 2014-01-08 18:16:17
6 2014-01-08 18:17:17
7 2014-01-08 18:18:17
8 2014-01-08 18:19:17
9 2014-01-08 18:20:17
You will not be able to have $vars stucked there, as it only works with plain db calls.
I'll give a practical example on a query i used to have running on my own game that was using events (yes this is the names for mysql jobs).
I have redrawn the bank system to it's own table and have the event running once a week, and you might be asking how do i make calculations on interest right? i have an extra field that shows the users bank interest (my system allows about 7 different interest rates) so the only thing i need is to call the field to make interest calculations and the query would be set like this.
Code: Select all
CREATE EVENT bank_interest
ON SCHEDULE EVERY 1 WEEK DO
UPDATE uBANK SET bMONEY = bMONEY + floor( bINTEREST * bMONEY / 100 ) WHERE bMONEY > 100
And finally... the resource on MySQL website can be found here... http://dev.mysql.com/doc/refman/5.7/en/ ... event.html