MySQLi物件導向實踐---交易處理

來源:互聯網
上載者:User

標籤:物件導向   exception   work   fail   catch   echo   get   message   class   

在看這篇部落格的時候,如果你有什麼疑惑嗎,可以參照一下這兩篇部落格

MySQLi面向過程實踐---交易處理http://www.cnblogs.com/-beyond/p/7577232.html

PDO實現交易處理http://www.cnblogs.com/-beyond/p/7551177.html

MySQLi物件導向的交易處理涉及以下三個函數

public bool mysqli::begin_transaction ([ int $flags [, string $name ]] )bool mysqli::commit ( void )bool mysqli::rollback ( void )

範例程式碼如下:

 最重要的莫過於關閉自動認可

 1 <?php  2     $db=new mysqli("localhost","root","root","test"); 3  4     $db->begin_transaction(); 5     $db->autocommit(FALSE);//重要 6     try{ 7         $sql_1="delete from aaa where uid = 2;"; 8         $db->query($sql_1); 9         if($db->affected_rows<=0){10             throw new Exception("failed to delete data one\n");11             //如果操作一失敗,則拋出異常,不會繼續執行下個操作12         }13 14         $sql_2="delete from aaa where uid = 999";15         $db->query($sql_2);16         if($db->affected_rows<=0){17             throw new Exception("failed to delete data two\n");18             //如果操作2失敗,則拋出異常19         }20 21         if(!$db->commit()){22             throw new Exception("failed to commit\n");23             //如果提交操作出錯,則拋出異常24         }25         echo "success to delete two data\n";26     } catch (Exception $e){27         $db->rollback();28         echo $e->getMessage();29         echo "haven‘t finish all works\n";30     }31 32     $db->close();33 ?>

 

MySQLi物件導向實踐---交易處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.