tp中怎么执行mysql事务_tp中使用事务

是什么

事务是为了防止,多个操作,其中有失败,数据有部分被执行成功的时候使用的。

比如,银行,用户转账。张三钱扣了,结果李四钱还增加!

这个时候需要使用事务,确保张三钱扣了,李四的钱也增加,才真正的成功!

能干嘛

确保数据的一致性!

如何使用呢?

/**

* 启动事务

* @access public

* @return void

*/

public function startTrans() {

$this->commit();

$this->db->startTrans();

return ;

}

/**

* 提交事务

* @access public

* @return boolean

*/

public function commit() {

return $this->db->commit();

}

/**

* 事务回滚

* @access public

* @return boolean

*/

public function rollback() {

return $this->db->rollback();

}

使用任何的model对象都可以开启。

// 开启事务

$mgoodsModel->startTrans();

$errcount = 0;

...

if ($errcount == 0) {

// 提交事务

$mgoodsModel->commit();

$this->outData['code'] = 1;

$this->outData['msg'] = '添加成功';

$this->printOut();

} else {

// 事务回滚

$mgoodsModel->rollback();

$this->outData['code'] = 2;

$this->outData['msg'] = '添加失败';

$this->printOut();

}

小结。人生在于折腾,编程在于折腾,工作在于折腾。折腾过的东西,才属于你。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值