Automatic Event Scheduler

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bwv2
Forum Commoner
Posts: 83
Joined: Fri Jun 10, 2005 11:50 am
Location: AZ

Automatic Event Scheduler

Post by bwv2 »

I'm designing a site that allows subscribers to use a service/tool. Each user has their own line in a database that keeps track of their user data. Each time they use the program, a counter is actuated to show that they used the program again, and it's written to the table. At the end of the month, the user is to be charged for all of their uses for that month, then the table field for the counter is reset to 0 for the next month.

I need to make a code that automatically charges all of the users in the database at a certain time of a certain date every month. For example, the users will know that at 11:59PM on the 25th of each month, their account will be charged. Now, I don't want to be awake at 11:59 on the 25th of every month to press a button to charge everyone. I need to make it automatic.

However, I'm not sure how to set up an automatic event like that. Is it even possible without activating the page first? I can see a number of cumbersome ways to code such a tool, but they would use a lot of wasted energy, and probably wouldn't work on their own without being prompted. Anyone know what commands I should look at for something like this?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Best bet is a cron job. I'm no expert but unless you want to search around here/Google for some info on cron jobs just wait around til someone who knows anything about them replies :)

There are indeed some cumbersome ways, but if I'm thinking the same as you they are best avoided.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Re: Automatic Event Scheduler

Post by santosj »

CRON Job

If you are running under Linux (use scheduler on Windows), you can create a crontab entry that will run a PHP script on the 25th of each month. As there are many, many resources on Cron jobs and Crontab, I'm not going to go into it right now.

MySQL 5.1 EVENT

There is a neat feature in MySQL that allows you to setup an event that you can schedule for every 25th of each month. The nice thing about it is that since it is on the mysql server it will be hella fast and since the instance of the mysql server will always running you'll have no problems with it.

Event is in version 5.0 but I believe from the article I read, that it wasn't really ready for prime time. From my reading, you can create any number of named events up to 64 characters with one SQL statement. The syntax can be found at the mysql document site.

However, I would also advise against (along with most of the senior forum members) using such a method, since you really have no control over the SQL text. If you only want to reset the counter to 0 then it would be fine, however you may also want to bill them for the amount on the counter which would be difficult unless you used SQL. It would give you better control to use a cron job and a PHP script for such a task.

I just wanted to let people know about Mysql Event since I believe it to be pretty awesome.
Post Reply