require_once "db.php"; $redis = new redis(); $redis->connect("127.0.0.1",6379); $limit = 1; //库存3个 $redis->watch("watchkey");//命令监事、监视销量 $watchkey = $redis->get("watchkey"); ///获取一个建的值 if($watchkey >= $limit){ echo "活动结束"; exit; } $redis->multi(); //开启事务 $redis->set("watchkey",$watchkey+1); //成功了才+1 sleep(2); $result = $redis->exec(); //返回成功或者失败 if($result){ //准备SQL语句 $sql = "select * from zichaxun where name='pc'"; //执行SQL语句 $query = $con->query($sql); $arr = $query->fetch_array(); if($arr["num"]>0){ $sql = "update zichaxun set num = num -1 where name ='pc'"; $res = $con->query($sql); if($res){ echo "库存减少"; }else{ echo '失败'; } }else{ echo "没有库存"; } //释放资源 $query->close(); //关闭连接 $con->close(); }else{ echo "失敗"; }