Urgently processing concurrent PHP code for a database

Source: Internet
Author: User
Urgently ask for a piece of database processing concurrent PHP code a book How to prevent two people from buying the following table book (id, name, num) num is Inventory such as (1, & quot; advanced Language & quot;, 1) in this book, how can I change the inventory and use transactions to buy one of them at a certain time ??? I just got in touch with php for details !, A piece of database processing concurrent PHP code needs to be urgently asked for help
How to prevent two people from buying a book
As follows:
Table book (id, name, num) num is Inventory
For example, (1, "Advanced Language", 1) when a book is published, many people click to buy it at a certain time, so that one of them can buy it, and then modify the inventory.
How to implement it using transactions ???

Contact php for details!




------ Solution --------------------
Do I need to use transactions? Everything is under your control.
------ Solution --------------------
The code is written by you. How can you control it. You can determine whether the buyer is "determined" to buy and modify the database inventory. Otherwise, you will not be able to buy it.
------ Solution --------------------
There is no need to consider this issue. The database automatically locks and does not modify data tables by two people at the same time. You can determine whether the number is 0 or not and cannot purchase it again. modify the database statement and return the judgment result.
------ Solution --------------------
UPDATE table SET num = 0 WHERE num> 0 AND id = 888;
If the inventory is already 0, do you think it will be modified?
------ Solution --------------------
Discussion

Reference:

Reference:

UPDATE table SET num = 0 WHERE num> 0 AND id = 888;
If the inventory is already 0, do you think it will be modified?

Yes.

But insert

------ Solution --------------------
Use another table to record who bought the record
------ Solution --------------------
Discussion

Reference:

Reference:

Reference:

Reference:

UPDATE table SET num = 0 WHERE num> 0 AND id = 888;
If the inventory is already 0, do you think it will be modified?

Yes.

But insert


You ......

------ Solution --------------------
Heated discussion ~
------ Solution --------------------
Set autocommit = 0, insert, update commit

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.