Emergency database processing of concurrent PHP code

Source: Internet
Author: User
Emergency SOS a database handles concurrent PHP code
How to prevent two people from buying a book
As follows
Table book (id,name,num) num is stock
For example (1, "high-level language", 1) such a book, at a time when many people click to buy, so that one of them to buy, buy after the change in inventory
How does a transaction work???

Just contact PHP, ask for details!




------Solution--------------------
Do you need to use transactions? It's all under your control.
------Solution--------------------
The code is written by you. You can control how you want to control it. You determine whether the buyer is "default" is to let it buy, modify the database inventory. Otherwise, do not let buy it.
------Solution--------------------
There is no need to consider this problem the database automatically has the lock function, does not appear at the same time two person modifies the data table the thing. Determine if the quantity is 0 zero and no more can be purchased. Modify the database statement to return the result of the decision.
------Solution--------------------
UPDATE table SET num=0 WHERE num>0 and id=888;
If the inventory is already 0, do you think he will change it?
------Solution--------------------
discuss

Reference:

Reference:

UPDATE table SET num=0 WHERE num>0 and id=888;
If the inventory is already 0, do you think he will change it?

Really can

But what I'm using, insert

------Solution--------------------
Who bought the records, please use another table to record
------Solution--------------------
discuss

Reference:

Reference:

Reference:

Reference:

UPDATE table SET num=0 WHERE num>0 and id=888;
If the inventory is already 0, do you think he will change it?

Really can

But what I'm using, insert


You......

------Solution--------------------
The discussion is so intense ~
------Solution--------------------
Set autocommit = 0, insert, Update commit
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.