View Full Version : php database calls

03-03-2012, 02:24 AM
im trying to charge customers for a recurring service using a php script. The script must check many customer database fields to determine who to charge and how much. I'm just confused on the "best practices" that would be used in this scenario.
Should i make one huge select statement to the SQL database for each client and store all their info temporarily in arrays while working on them, and then one huge update statement at the end? Im afraid if i start manipulating things one at a time i might have a problem with credit card acceptance (or anything else), and have to somehow go back and recreate the customer's previous state.
Or am i using entirly the wrong approach?
It hard enough to learn all these technologies let alone understand where to use which and when:confused:
Any direction would be appreciated, let me know if you need more detail to answer.


03-03-2012, 04:39 AM
I think it depends on what the criteria is for charging the customer.

After you successfully charge them, what changes in their database row
that satisfies them until the next time they're charged? Describe how you
determine who to charge.

I would create a "charge" script that executes automatically every hour.
It finds the first row that needs to be charged, based on your criteria. It processes
that charge and finishes. An hour later, it finds the next one ... and next ... etc.
Most of the time, it may not find any charges to process.

So, it will automatically charge up to 24 customers each day. Not sure how many
customers you have, but it will just keep cycling through every hour every day.

A PHP script can be automatically executed by using your webhosts CRON service.
You set the time intervals, days, etc. Maybe you only want charges to be made
Mon-Fri, but not on weekends.

You can also create more columns in your database to handle the "pending" charge,
to make sure the transaction is successful before "finalizing" the charge. When it is
finalized, that customer will no longer meet the criteria for being charged. The next
time the CRON runs, it will pick the next customer that meets the criteria.