Re: just a php/mysql logic question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dave Carrera wrote:
If multiple users hit the php app at the same time how do i ensure that the correct amount of stock is taken from stock so that a users does not accidentally sell from stock which has already been sold.

Even though multiple users may hit the PHP app "at the same time" (even though single processor machines can only actually do one thing at a time anyway), they can't all access the tables at the same time.

MySQL does something called table locking, which means that if you're updating a table then other clients SELECT statements for the same rows will wait until the table has finished being updated (usually not many milliseconds...)

This means that if you have something like:

UPDATE stock_table SET stock_count=stock_count-1 WHERE id=935882

and someone else hits it at the same timeand asks:

SELECT stock_count FROM stock_table WHERE id=935882

MySQL won't answer until the UPDATE statement has finished. You likely wouldn't even notice the delay though.

In short, don't worry about it unless you're doing more complex things where a bunch of statements need to be either all done at once, or not done at all. In that case you might like to look in to making your tables InnoDB and using the transaction features of MySQL.

--
Jasper Bryant-Greene
Freelance web developer
http://jasper.bryant-greene.name/

If you find my advice useful, please consider donating to a poor
student! You can choose whatever amount you think my advice was
worth to you. http://tinyurl.com/7oa5s

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux