(1).创建数据库test ,创建表shop(字段id,total),商品id是1,商品总数10

(2).PHP模拟购买,商品数量大于0才能购买
<?php
//连接数据库
$con=mysqli_connect("192.168.2.186","root","root","test");
//查询商品数量是否大于0
$res=mysqli_fetch_assoc(mysqli_query($con,'SELECT total FROM shop WHERE id=1 LIMIT 1'));
if($res['total']>0){
mysqli_query($con,'UPDATE shop SET total=total-1 WHERE id=1');
}
unset($res);
mysqli_close($con);
?>(3).ab.exe进行高并发测试
ab -n 500 -c 500 http://test.cn/
(4).数据库结果:

高并发下这样完全不行
(5).我们可以使用where条件来处理,更新的时候加上where条件即可,更新的时候更新失败说明库存已经不足
<?php
//连接数据库
$con=mysqli_connect("192.168.2.186","root","root","test");
//查询商品数量是否大于0
$res=mysqli_fetch_assoc(mysqli_query($con,'SELECT total FROM shop WHERE id=1 LIMIT 1'));
if($res['total']>0){
mysqli_query($con,'UPDATE shop SET total=total-1 WHERE id=1 and total>0');
}
unset($res);
mysqli_close($con);
?> <?php //对比$this和self /* * $this更倾向于对象本身 * */ class Par{ public  ...
<!doctype html> <html> <head> <meta charset="utf-8"> <title>demo</title> </head> <bod...
<?PHP $a=0.5*100; if(is_int($a)){ echo'int'; } else{ echo'not int'; } ...
使用php函数array_multisort()即可实现和SQL一样的order by排序. 例如我们需要对会员表按照主键降序排列,年龄升序排列://会员表数据 $list = []; $list[] = ['mid' =>&n...
Redis提供了发布订阅功能,可以用于消息的传输,Redis的发布订阅机制包括三个部分,发布者(publisher),订阅者(subscriber)和频道(channel)。 发布者和订阅者都是Redis客户端,Channel则为Redis服务器端,发布者将消息发送到某个的频道,订阅了这个...
(1).config.php 配置文件<?php /** * RabbitMQ_Config */ $config = [ 'host' => ...