Ok, let's say we have bank_account
table. There is a row called money
. It has 2,000 value. And we have two persons trying to withdraw that money, let's suppose they can do it. So, is it possible that they would do it at the same time? For example. there is a made-up code:
$all_money = get_value('money', 'bank_account); //lets suppose thats a function how we get value money from bank_account table
if($all_money > 0)
{
//here is a code where those money are being withdrawed from bank_account and inserting into the person's account
}
If those two persons get value 2,000 at the same time then it means this clause if($all_money > 0)
will be executed and both players will get 2,000 and bank_account will have -2,000 value of money. So, is it possible to happen? If yes, then how could I protect it? Thank you.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With