DEMO code of PHP + MySQL transaction operations

Source: Internet
Author: User

The following articles mainly describe the actual application code demonstration of PHP + MySQL transaction operations. We all know that in actual LAMP applications, generally, PHP uses AdoDB to operate MySQL databases. The corresponding code of AdoDB is provided below for your convenience:

 
 
  1. <?php   
  2. // ...   
  3. $adodb->startTrans();   

Actually, the query called by getOne can also be directly placed in rowLock. This is only for demonstration of more obvious results.

 
 
  1. $adodb->rowLock('book', 'book_id = 123');   
  2. $bookNumber = $adodb->getOne("SELECT book_number FROM book WHERE book_id = 123");   
  3. $adodb->execute("UPDATE book SET book_numberbook_number = book_number - 1 WHERE book_id = 123");   
  4. $adodb->completeTrans();   
  5. // ...   
  6. ?> 

The rowLock method is the row lock implemented by the for update method. You may want to write "for update" directly to $ adodb-> getOne () the SQL statement called to implement the row lock function is good, it is indeed OK, but not all databases use the "FOR UPDATE" syntax to implement the row lock function, for example, Sybase uses the "HOLDLOCK" syntax to implement the row lock function. To ensure portability of your database abstraction layer, I suggest you use rowLock to implement the row lock function, as for portability, you can leave it to AdoDB. Well, it's a bit far away. I'll talk about it now.

The above content is a description of the Code demo for PHP + MySQL transaction operations. I hope it will help you in this regard.

Appendix:

In AdoDB, there is a setTransactionMode () method that can set the transaction isolation level, as shown below:

 
 
  1. SetTransactionMode allows you to pass in the transaction mode to use for all 
    subsequent transactions for that connection session. Note: if you have persistent 
    connections and using mysql or mssql, you might have to explicitly reset your 
    transaction mode at the beginning of each page request. This is only supported in postgresql, 
    mssql, mysql with InnoDB and oci8 currently. For example:  
  2. $db->SetTransactionMode("SERIALIZABLE");  
  3. $db->BeginTrans();  
  4. $db->Execute(...); $db->Execute(...);  
  5. $db->CommiTrans();  
  6. $db->SetTransactionMode(""); // restore to default  
  7. $db->StartTrans();  
  8. $db->Execute(...); $db->Execute(...);  
  9. $db->CompleteTrans();  
  10. Supported values to pass in:  
  11. * READ UNCOMMITTED (allows dirty reads, but fastest)  
  12. * READ COMMITTED (default postgres, mssql and oci8)  
  13. * REPEATABLE READ (default mysql)  
  14. * SERIALIZABLE (slowest and most restrictive)   

The above content is an introduction to the Code demonstration of PHP + MySQL transaction operations. I hope you will gain some benefits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.