Jump to content

Recommended Posts

In other words, if I have a piece of code doing an important database update, how many doublechecks do I need to do?

 

The script in question is a PHP script that gets executed by cron every night to accrue interest on all of our accounds. It has been working fine but doesn't really have much error checking in place. It loops through each account in our database and executes the following for each of them:

 

  $query = mysql_query("UPDATE dbase SET accruedinterest=$newAccruedInterest, currentbalance=$newCurrentBalance,
custom58='" . date('j') . "'  WHERE id=" . $row['id']) or die(mysql_error()); 

 

Now, this seems fairly bulletproof. 'id' is the PK of the table and is flagged unique. Would it be a waste of resources to do this update in a transaction and make sure exactly one row is being updated?

Link to comment
https://forums.phpfreaks.com/topic/191655-how-anal-is-too-anal/
Share on other sites

When money is concerned you can never be too anal about correctness.  You never, ever want the blame finger to land on you when money goes missing from the company's coffers.

 

Interestingly enough there doesn't seem to be a simple way to be sure that update statements work correctly.

 

1) Checking for errors is not enough.  Sure if errors occur you can assume the update did not work.  But just because there are no errors doesn't mean it worked either!  For example if you filtered on the wrong column or value; the query will work without errors but it doesn't do what you wanted.

 

2) You might be tempted to use a function like mysql_affected_rows() to count the number of rows updated.  But that won't necessarily work either!  The reason is because it will only return rows that actually changed.  If no update was necessary (and the row did not change) it won't be included in the tally returned by mysql_affected_rows().

 

It seems to me the only sure way to know the rows were updated is to perform count(*) queries before and after and make sure the number of final rows with those values are what you expect to them to be.

 

You'll also want to sacrifice performance when doing financial calculations and make sure the database is not buffering writes to disk.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.