1 <?php 2 $id = 1; 3 $pdo=new PDO("mysql:host=127.0.0.1;dbname=test","root","root"); 4 $pdo->exec("UPDATE `ms_goods` SET `nums`=`nums`-1 WHERE id=$id "); 5 echo "miao sha success"; 6 // 队列 链表 list :array 7 // array:先进后出 8 // list: 先进先出 9 ?>
1 <?php 2 // 库存复数 : 并发量足够大 3 $r=new Redis(); 4 5 $r->connect("127.0.0.1",6379); 6 7 $res=$r->rpop("mslist"); //right 右:出队 ---> 8 9 if(!$res){ 10 echo "out of print"; 11 }else{ 12 $id = 1; 13 $pdo=new PDO("mysql:host=127.0.0.1;dbname=test","root","root"); 14 $pdo->exec("UPDATE `ms_goods` SET `nums`=`nums`-1 WHERE id=$id "); 15 echo "miao sha success"; 16 } 17 18 // 队列 链表 list :array 19 // array:先进后出 20 // list: 先进先出 21 ?>
1 <?php 2 $id = 1; // 活动id 3 $pdo=new PDO("mysql:host=127.0.0.1;dbname=test","root","root"); 4 $num = $pdo->query("SELECT * FROM `ms_goods` WHERE id = $id ")->fetch(); 5 $nums = $num['nums']; //库存 6 7 // 库存复数 : 并发量足够大 8 $r=new Redis(); 9 10 $r->connect("127.0.0.1",6379); 11 12 for($i=1;$i<=$nums;$i++){ 13 $r->lpush("mslist","$i"); //left 左:入队 ----> 14 } 15 16 echo "[nums:{$nums}] list: miaosha start ..."; 17 ?>
1 <?php 2 $id = 1; 3 $pdo=new PDO("mysql:host=127.0.0.1;dbname=test","root","root"); 4 $res=$pdo->query("SELECT * FROM `ms_goods` where id = $id")->fetch(PDO::FETCH_ASSOC); 5 ?> 6 7 <!DOCTYPE html> 8 <html lang="en"> 9 <head> 10 <meta charset="UTF-8"> 11 <title>秒杀功能</title> 12 </head> 13 <body> 14 15 <center> 16 <h1>抢购活动,热卖,促销</h1> 17 <div style="320px;height:160px;border:1px solid #ccc;"> 18 <img src="tel_5.jpg" /> 19 </div> 20 <p>库存:<?php echo $res['nums']; ?></p> 21 <button id="msbtn">秒杀</button> 22 </center> 23 24 </body> 25 </html> 26 27 <script type="text/javascript"> 28 document.getElementById('msbtn').onclick=function(){ 29 window.location.href = 'doms.php'; 30 } 31 </script>