這篇文章主要介紹了詳解MySQL中關於事務的處理,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟著小編過來看看吧
步驟:
#1.開啟事務 start transaction
當我們開啟一個事務的時候,我們對sql的操作都發生在記憶體中,但是沒有真正的回饋到資料庫磁碟的檔案中!
2.回滾 rollback
#回滾,就是恢復到交易開啟之前的最原始的狀態!
注意:回滾操作會自動的關閉一個事務,如果想再次執行事務,需要重新開啟事務!
3.提交 commit
#
普通的執行,之所以是立即執行並生效,因為預設的,MySQL對sql語句的執行是自動提交的!所以,開啟一個事務的本質,就是關閉了先前的自動提交的功能,而是由使用者手動提交(利用commit語句)!
總結交易的步驟:
1, 開啟交易
2, 若執行成功,就提交commit
3, 若有任何一條sql語句執行失敗,則回滾rollback!
交易處理最典型的就是藉錢。 下面以張三向李四還1000元為例
先查看資料庫中各自的錢數
##下面是處理還錢交易的程式碼:
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'><?<span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;">php </span><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">/** * MySQL实现事务操作<br/>*/<br/><br/></span><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">echo</span> "<meta charset=utf-8>"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 1 连接数据库<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$link</span> = @<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_connect</span>('localhost','root','') or <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>die</span>('连接数据库失败'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_select_db</span>('test',<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$link</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>('set names utf8'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 2 开启事务<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>("start transaction"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//设置一个变量,用来判断所有sql语句是否成功<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>true</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 2.1执行事务中的一组sql语句 <br><br>// 李四的money+1000<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span> = "update pdo set mone=money+1000 where name='李四'"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>(<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>if</span> (!<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>) { <br> </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//若sql语句执行失败,把$falg设置为false</span> <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>false</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>; }<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//张三的money-1000<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span> = "update pdo set money=money-1000 where name='张三'"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>(<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>if</span> (!<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>) { <br> </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//若sql语句执行失败,把$falg设置为false</span> <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>false</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>; }<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 2.2 判断事务是否执行成功<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>if</span> (<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>) { <br> </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//所有sql语句执行成功,把sql语句提交</span> <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>('commit'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>); </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>echo</span> "还钱成功!"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>; }</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>else</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>{ <br> </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 如其中一条执行失败,则回滚到事务开启之前的状态</span> <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>('rollback'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>); </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>echo</span> "还钱失败!"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>; }</span></span>
結果:
下面,我們故意把其中一個欄位寫錯,看看交易是否正常處理,資料庫中的錢數是否有變化!
<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>// 李四的money+1000<br>$sql = "update pdo set mone=money+1000 where name='李四'"; //把moeny字段错写成mone<br></span>
結果:
結果是還錢失敗,且資料庫中各自的錢數沒有變化,說明當某一條語句未執行成功時,事物不會提交,而會回滾,把資料恢復到開始事務之前的原始狀態,這也是使用事務的作用,即只有當交易中所有sql語句全部執行成功,交易才會提交,否則會回溯! #
以上是程式碼詳解MySQL中關於事務的處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!