+6  A: 

Execute this before accessing the table:

LOCK TABLE table_name READ;

That will lock the table. When you have finished doing the work, you call:

UNLOCK TABLES;
Cristian
+2  A: 

I think one approach would be to use transactions. When the transactions starts the table will get locked. Its actually similar to what Cristian proposed.

realshadow
Really? I actually use transactions and these are the great news if it's correct and somebody can approve that.
hey
I approve that.
Matthew
I am not sure if it does. http://dev.mysql.com/doc/refman/5.0/en/lock-tables-and-transactions.html
Scott Chamberlain
I don't think this is the correct way at all. I think your correct choice is Cristian C's suggestion.
northpole
+1  A: 

Transactions in innodb will help you with that. Remember to read up on your mysql version, because they were bugged earlier.

Android Noob
+3  A: 

Contrary to belief, SQL statements are received and placed in a queue for processing. They are not executed at the same time.

What happens depends on business rules - if a bank account allows overdraft, the second request will succeed (assuming large enough overdraft). Otherwise, the second request will fail.

OMG Ponies
+1 I would give you plus 2 for your name if I could...best.name.ever!
northpole
I should mention that Isolation Levels can complicate things - the default allows for priority to SELECT, meaning checking your balance could return the value before a withdrawl is made.
OMG Ponies
"[SQL statements] are not executed at the same time"? Do you want to further qualify this statements? Are you saying there is never any overlapping in the execution of any SQL statements? I hope not.
Artefacto
A: 

I think you are worried about 2 separate threads connecting to the database and one with-drawing money while the other is executing code that based on a balance read previously.

In this situation you should apply a read lock (as Christian says) prior to querying the balance and releasing the lock once [in your code snippet] the if statement has ended.

You may face problems if anything falls over while your thread still has the lock so you need some canny bash scripts to kill stuff ;)

ToonMariner
+1  A: 

A very stereotypical example would be:

DELIMITER //
CREATE PROCEDURE do_banktransfer(
  IN transfer_amount INT,
  IN from_account    INT,
  IN to_account      INT,
  OUT success        INT)
BEGIN
START TRANSACTION;
UPDATE account SET balance = balance - transfer_amount WHERE id = from_account;
UPDATE account SET balance = balance + transfer_amount WHERE id = to_account;
SELECT balance INTO cur_balance FROM account WHERE id = from_account;
IF cur_balance < 0 THEN
    SET success = 0;
    ROLLBACK;
ELSE 
    SET success = 1;
    COMMIT;
END IF;
END;//
Wrikken