首頁 > 後端開發 > php教程 > thinkphp 的事務回溯處理 和 原始PHP的事務回滾實例

thinkphp 的事務回溯處理 和 原始PHP的事務回滾實例

WBOY
發布: 2016-08-08 09:26:39
原創
1001 人瀏覽過

1.  要程式裡面支援事務,首先連接的資料庫與資料表必須支援事務 mysql   為例:

資料庫InnoDB支援 transactions

 

資料表支援事務:InnoDB  支援transaction

2. 框架thinkphp  支援事務代碼

<span>public</span> <span>function</span><span> testrollback(){ </span><span>$model1</span> = D('item'<span>); </span><span>$model2</span> = D('vote'<span>); </span><span>$model1</span>-><span>startTrans(); </span><span>$res1</span> = <span>$model1</span>->where('id = 5')-><span>delete(); </span><span>$res2</span> = <span>$model2</span>->where('id = 2')-><span>delete();
dump(</span><span>$res1</span><span>);
dump(</span><span>$res2</span><span>); </span><span>if</span>(<span>$res1</span> && <span>$res2</span><span>){ </span><span>$model1</span>->commit(); <span>//</span><span>只有$res1 和  $res2  都执行成功是才真正执行上面的数据库操作</span> dump("commit"<span>);
}</span><span>else</span><span>{ </span><span>$model1</span>->rollback(); <span>//</span><span> 条件不满足,回滚</span> dump("rollback"<span>);
}
dump(</span>"over"<span>); </span><span>exit</span><span>;
}</span>
登入後複製

 

 

 

3.  原始PHP 程式碼事務實例

方法一:只支援資料庫和資料表都是 innoDB  的情況

<span>public</span> <span>function</span><span> rollbackoriginal1(){ </span><span>$conn</span> = <span>mysql_connect</span>('127.0.0.1','summerzi','summerzi') or <span>die</span>('DB connection failed!'<span>); </span><span>mysql_select_db</span>('summer',<span>$conn</span><span>); </span><span>mysql_query</span>('set names "GBK"'<span>); </span><span>mysql_query</span>('BEGIN'<span>); </span><span>$sql1</span> = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(41,1,233);"<span>; </span><span>$sql2</span> = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(fdfd,2,235);"<span>; </span><span>$res1</span> = <span>mysql_query</span>(<span>$sql1</span><span>); </span><span>$res2</span> = <span>mysql_query</span>(<span>$sql2</span><span>);
        dump(</span><span>$sql1</span><span>);
        dump(</span><span>$sql2</span><span>);
        dump(</span><span>$res1</span><span>);
        dump(</span><span>$res2</span><span>); </span><span>if</span>(<span>$res1</span> && <span>$res2</span><span>){ </span><span>mysql_query</span>('COMMIT'<span>);
            dump(</span>'commit success!'<span>);
        }</span><span>else</span><span>{ </span><span>mysql_query</span>('ROLLBACK'<span>);
            dump(</span>'commit failed, rollback!'<span>);
        } </span><span>mysql_query</span>('END'<span>);

    }</span>
登入後複製

方法二:(注意:對於不支援交易的MyISAM引擎資料庫可以使用表格鎖定的方法)

<span>public</span> <span>function</span><span> rollbackoriginal2(){ </span><span>$conn</span> = <span>mysql_connect</span>('127.0.0.1','summerzi','summerzi') or <span>die</span>('DB connection failed!'<span>); </span><span>mysql_select_db</span>('summer',<span>$conn</span><span>); </span><span>mysql_query</span>('set names "GBK"'<span>); </span><span>mysql_query</span>('SET AUTOCOMMIT=0');<span>//</span><span>//设置mysql不自动提交,需自行用commit语句提交</span> <span>$sql1</span> = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(41,1,233);"<span>; </span><span>$sql2</span> = "INSERT INTO `summer_userdata`(`uid`,`type`,`target_id`) VALUES(44,2,235);"<span>; </span><span>//</span><span>mysql_query("LOCK TABLES `hmbl_userdata` WRITE");//锁定表</span> <span>$res1</span> = <span>mysql_query</span>(<span>$sql1</span><span>); </span><span>$res2</span> = <span>mysql_query</span>(<span>$sql2</span><span>);
        dump(</span><span>$sql1</span><span>);
        dump(</span><span>$sql2</span><span>);
        dump(</span><span>$res1</span><span>);
        dump(</span><span>$res2</span><span>); </span><span>//</span><span>mysql_query("UNLOCK TABLES");//解除锁定</span> <span>if</span>(<span>$res1</span> && <span>$res2</span><span>){ </span><span>mysql_query</span>('COMMIT'<span>);
            dump(</span>'commit success!'<span>);
        }</span><span>else</span><span>{ </span><span>mysql_query</span>('ROLLBACK'<span>);
            dump(</span>'commit failed, rollback!'<span>);
        } </span><span>mysql_query</span>("SET AUTOCOMMIT=1"<span>); </span><span>mysql_query</span>('END'<span>);
        

    }</span>
登入後複製

php + mysql  對事務的處理比較簡單,牽涉到業務中多個資料操作,就可以考慮用事務處理


以上就介紹了thinkphp 的交易回滾處理 和 原始PHP的事務回滾實例,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板