(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);
?> 上家公司开发医院挂号系统,系统采用GBK编码。ajax发送的中文用户名让PHP保存为cookie出现乱码的解决方案。1.Javascript变量var user=document.getElementById('user').innerText; user=escape(u...
在项目中需要对图片进行裁剪,前端裁剪完成发送base64给后端,但是很意外的PHP获取到的数据和前端有点差距,之前我都是先加密,后端解密,但是这次依然不行。于是使用filter_input方法轻松解决。$base64 = filter_input(INPUT_POST...
(1).前端文件:<form action="upload.php" method="post" enctype="multipart/form-data"> &...
php7新增的特性(1).强制限制只能返回一种类型<?php class task { } //must return an integer function add(): int { &nb...
概念请参考w3school文章: redis watch ,redis exec (看完基本秒懂)(1)基本事务://连接本地的 Redis 服务 $redis = new Redis(); $redis->con...
众所周知MyISAM引擎不支持事务,但是我只是知道不支持事务,并未测试具体的表现是什么,测试代码如下:try { //开启事务 Db::startTrans(); &...