Westcoast Sheri wrote:
[color=blue][color=green]
>>One of the problems with PHP is the fact that each database operation is ATOMIC.
>>- Stands alone. Because of this, I would build a database function that
>>derives the current value of quantity_left and decrements and saves the new
>>value. Otherwise, if you have more than one "cashier" then the possiblity of
>>"missing" a transaction increases significantly.[/color]
>
>
> What does that mean? Am I correct in now assuming that you mean that this is bad:
>[/color]
It means that you need to read up on what constitutes a transaction to insure
data integrity.
[color=blue]
> $link = mysql_connect('localhost','user','pass');
> mysql_select_db('database',$link);
> $increment = "update fruit_table set fruit = fruit - ".$number_sold." where fruit =
> '".$type_of_fruit."'";
> mysql_query($increment,$link);
>
> .... and that it would be better to do this:
>
> $link = mysql_connect('localhost','user','pass');
> mysql_select_db('database',$link);
> // do code to select (obtain) quantity from mysql database
> // then use PHP to decrement the quantity
> // then use a mysql statement to insert the new quantity into database
>[/color]
no. It means that in your code you have 3 seperate and distinct transactions
that could result in erroneous data.
Using your code:
user1:
query - get current quantity of apples (=100)
at the same time
user2:
query - get current quantity of apples (still = 100)
user1
calculate 100 - 8
user2
calculate 100 - 6
user1
updates quantity (=92)
user2
updates quantity (=94)
user1 exit
user2 exit
Because EACH is in a seperate TRANSACTION you now have the possibility of being
off by 8.
What if you happen to make a bank transaction and this occurred while you were
making it? -- Well in this case you would win, but what one was adding money
and another subracting it... you would come up short.
If you do not understand what constitutes a "transaction" in a database and how
they are implemented in a "scripting" language like PHP, you cannot write code
that will work 100% of the time.
Because with PHP, each statement is considered a seperate transaction - unless
you use mysql_query("BEGIN|COMMIT|ROLLBACK"), the locking mechanisms that
prevent this senerio are NOT engaged. Or by using a DATABASE/MySQL user defined
FUNCTION that:
gets the current value
increment or decrements it
updates the current with the new
withing a SINGLE transaction, then you may end up with the wrong quanitities or
dollar values...
From the PHP docs:
"Regarding transactions, you must use a recent MySQL version which supports
InnoDB tables. you should read the mysql manual (the part about Innodb tables,
section 7.5) and configure your server to use them.
Some reading about how it works:
http://php.weblogs.com/discuss/msgReader$1446?mode=topic
(Click where it says Part2, I can't put the direct URL here because it is too long)
Then in PHP you use commands like:
mysql_query("BEGIN");
mysql_query("COMMIT");
mysql_query("ROLLBACK");
You must make sure that you convert your existing tables to innodb or create new
ones: CREATE TABLE (...) type=innodb;"
This will ensure that the 3 statements are a part of the same transaction, not
seperate transactions.
--
Michael Austin.
System Analyst and DBA
Donations welcomed.
Http://www.firstdbasource.com/donations.html
:)