mysql - 关于php开发抢购程序涉及到的问题
我没事做了个小程序,主要的功能是用户抢购商品,比如一个商品标价是40元,每个用户出1元,用户抢购记录的总数等于40,那么这个商品就关闭购买。
我有个疑虑,如果用户多并发怎么办呢,如果同一时刻有10个用户同时抢购并付款,怎么保证购买记录表中的购买总数不超过商品总价呢?
如果商品价值40,已经有36个人成功付款并购买,还剩下4个,这是又有10个用户都想抢剩下的4个名额,那就说有6个人是抢不到的,那么怎么控制呢?不让总数溢出···
谢谢各位啦~~
有人说是数据库加锁,不知道什么意思···
回复内容:
我没事做了个小程序,主要的功能是用户抢购商品,比如一个商品标价是40元,每个用户出1元,用户抢购记录的总数等于40,那么这个商品就关闭购买。
我有个疑虑,如果用户多并发怎么办呢,如果同一时刻有10个用户同时抢购并付款,怎么保证购买记录表中的购买总数不超过商品总价呢?
如果商品价值40,已经有36个人成功付款并购买,还剩下4个,这是又有10个用户都想抢剩下的4个名额,那就说有6个人是抢不到的,那么怎么控制呢?不让总数溢出···
谢谢各位啦~~
有人说是数据库加锁,不知道什么意思···
update Item set count=count+1 where id=$id and count=$count;
大致逻辑:
try=0;
while true do
select * from item where id=1;
if $count >=40 then close item; break end
if ++try >10 then break end
update item count=count+1 where id=$id and count=$count;
if update ok then
xxxx
break;
end
end
这种应用场景可以考虑memcache或redis。
mysql高并发下就得使用锁机制了。
InnoDB引擎,该引擎支持行锁,支持事务,外键。
可以使用 for update 来共享锁,结合事务如下。
begin;
select price from good where id=1 for update;//查询当前商品的价格 for update 一定要加上
然后判断是否达到40
没有达到40执行
1 2 3 |
|
达到的话就回滚终止
1 |
|
锁是计算机协调多个进程或线程并发访问某一资源的机制,为了保证数据的一致性和有效性。
锁分为表锁、行锁还有页面锁,高并发的环境我推荐用行锁。
表级锁
开销小,加锁快;不会出现死锁;锁定粒度大,发生锁冲突的概率最高,并发度最低;适合查询操作较多
行级锁
开销大,加锁慢;会出现死锁;锁定粒度最小,发生锁冲突的概率最低,并发度也最高。适合并发更新数据
页面锁
开销和加锁时间介于表锁和行锁之间;会出现死锁;锁定粒度介于表锁和行锁之间,并发度一般。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.
