php+odbc能实现事务进行高并发下单,防止库存不足?怎样把mysql换成odbc 2C

php+odbc能实现事务进行高并发下单,防止库存不足?怎样把mysql换成odbc
<?php

$conn=mysql_connect("localhost","big","123456");

if(!$conn){

echo "connect failed";

exit;

}

mysql_select_db("big",$conn);

mysql_query("set names utf8");
$price=10;

$user_id=1;

$goods_id=1;

$sku_id=11;

$number=1;
//生成唯一订单号

function build_order_no(){

return date('ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

}

//记录日志

function insertLog($event,$type=0){

global $conn;

$sql="insert into ih_log(event,type)

values('$event','$type')";

mysql_query($sql,$conn);

}
//模拟下单操作

//库存是否大于0

mysql_query("BEGIN"); //开始事务

$sql="select number from ih_store where goods_id='$goods_id' and sku_id='$sku_id' FOR UPDATE";//此时这条记录被锁住,其它事务必须等待此次事务提交后才能执行

$rs=mysql_query($sql,$conn);

$row=mysql_fetch_assoc($rs);

if($row['number']>0){

//生成订单

$order_sn=build_order_no();

$sql="insert into ih_order(order_sn,user_id,goods_id,sku_id,price)

values('$order_sn','$user_id','$goods_id','$sku_id','$price')";

$order_rs=mysql_query($sql,$conn);
//库存减少

$sql="update ih_store set number=number-{$number} where sku_id='$sku_id'";

$store_rs=mysql_query($sql,$conn);

if(mysql_affected_rows()){

insertLog('库存减少成功');

mysql_query("COMMIT");//事务提交即解锁

}else{

insertLog('库存减少失败');

}

}else{

insertLog('库存不够');

mysql_query("ROLLBACK");

}
?>
-- 数据库: big
-- 表的结构 ih_goods
CREATE TABLE IF NOT EXISTS ih_goods (

goods_id int(10) unsigned NOT NULL AUTO_INCREMENT,

cat_id int(11) NOT NULL,

goods_name varchar(255) NOT NULL,

PRIMARY KEY (goods_id)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
-- 转存表中的数据 ih_goods
INSERT INTO ih_goods (goods_id, cat_id, goods_name) VALUES

(1, 0, '小米手机');
-- 表的结构 ih_log
CREATE TABLE IF NOT EXISTS ih_log (

id int(11) NOT NULL AUTO_INCREMENT,

event varchar(255) NOT NULL,

type tinyint(4) NOT NULL DEFAULT '0',

addtime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

PRIMARY KEY (id)

) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
-- 转存表中的数据 ih_log
-- 表的结构 ih_order
CREATE TABLE IF NOT EXISTS ih_order (

id int(11) NOT NULL AUTO_INCREMENT,

order_sn char(32) NOT NULL,

user_id int(11) NOT NULL,

status int(11) NOT NULL DEFAULT '0',

goods_id int(11) NOT NULL DEFAULT '0',

sku_id int(11) NOT NULL DEFAULT '0',

price float NOT NULL,

addtime timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='订单表' AUTO_INCREMENT=1 ;
-- 转存表中的数据 ih_order
-- 表的结构 ih_store
CREATE TABLE IF NOT EXISTS ih_store (

id int(11) NOT NULL AUTO_INCREMENT,

goods_id int(11) NOT NULL,

sku_id int(10) unsigned NOT NULL DEFAULT '0',

number int(10) NOT NULL DEFAULT '0',

freez int(11) NOT NULL DEFAULT '0' COMMENT '虚拟库存',

PRIMARY KEY (id)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='库存' AUTO_INCREMENT=2 ;
-- 转存表中的数据 ih_store
INSERT INTO ih_store (id, goods_id, sku_id, number, freez) VALUES

(1, 1, 11, 500, 0);

0

1个回答

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!