tp中怎么执行mysql事务_tp中使用事务
是什么
事務(wù)是為了防止,多個(gè)操作,其中有失敗,數(shù)據(jù)有部分被執(zhí)行成功的時(shí)候使用的。
比如,銀行,用戶轉(zhuǎn)賬。張三錢扣了,結(jié)果李四錢還增加!
這個(gè)時(shí)候需要使用事務(wù),確保張三錢扣了,李四的錢也增加,才真正的成功!
能干嘛
確保數(shù)據(jù)的一致性!
如何使用呢?
/**
* 啟動事務(wù)
* @access public
* @return void
*/
public function startTrans() {
$this->commit();
$this->db->startTrans();
return ;
}
/**
* 提交事務(wù)
* @access public
* @return boolean
*/
public function commit() {
return $this->db->commit();
}
/**
* 事務(wù)回滾
* @access public
* @return boolean
*/
public function rollback() {
return $this->db->rollback();
}
使用任何的model對象都可以開啟。
// 開啟事務(wù)
$mgoodsModel->startTrans();
$errcount = 0;
...
if ($errcount == 0) {
// 提交事務(wù)
$mgoodsModel->commit();
$this->outData['code'] = 1;
$this->outData['msg'] = '添加成功';
$this->printOut();
} else {
// 事務(wù)回滾
$mgoodsModel->rollback();
$this->outData['code'] = 2;
$this->outData['msg'] = '添加失敗';
$this->printOut();
}
小結(jié)。人生在于折騰,編程在于折騰,工作在于折騰。折騰過的東西,才屬于你。
總結(jié)
以上是生活随笔為你收集整理的tp中怎么执行mysql事务_tp中使用事务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 中国的985211大学名单 985211
- 下一篇: mysql 时间 1_(转)mysql