转:http://developer.51cto.com/art/200912/166409.htm
http://blog.csdn.net/caomiao2006/article/details/38568825
1 <?php
2 $conn=mysql_connect("localhost","root","root");
3 $db=mysql_select_db("indextest");
4 mysql_query("BEGIN");
5 $addtime=time();
6 try{
7 $sql="update product set storenum=storenum-1 where product_id=1";
8 echo $sql."<br>";
9 mysql_query($sql);
10 $sql="insert into `order` set product_id=1,addtime='$addtime'";
11 mysql_query($sql);
12 $sql2="select storenum from product where 1 and product_id=1";
13 echo $sql2."<br>";
14 $query2=mysql_query($sql2);
15 $row2=mysql_fetch_array($query2);
16 echo "<pre>";
17 var_dump($row2);
18 echo "</pre>";
19 $num=$row2["storenum"];
20 echo "num=>".$num."<br>";
21 if($num<0||$num==""){
22 //echo "error";
23 throw new Exception('库存不足');
24 }
25 }catch(Exception $e){
26 echo "test";
27 mysql_query("ROLLBACK");
28 }
29 mysql_query("COMMIT");
30