Actual application code Demo for PHP + MySQL transaction operation

Source: Internet
Author: User
Tags php tutorial

The following article mainly describes the PHP (PHP training PHP tutorial) + mysql (MySQL certified MySQL training) transaction operation of the actual application code demonstration, we all know in the actual lamp application, PHP is generally used by the ADODB to operate the MySQL database, the following gives ADODB the corresponding code for easy access to everyone:

Starttrans ();

In fact, the query called by GetOne can also be placed directly into the rowlock, here just to demonstrate the effect can be more obvious.

$adodb->rowlock (' book ', ' book_id = 123 '); $bookNumber = $adodb->getone ("Select Book_number from book WHERE book_id = 123"); $adodb->execute ("UPDATE book SET book_numberbook_number = book_number-1 WHERE book_id = 123"); $adodb->completetrans (); // ... ? >

Where the Rowlock method is to invoke the for update to implement the row lock, you may want to "for update" directly into the $adodb->getone () called the SQL statement to implement the row lock function, yes, that is true, However, not all databases use the "for UPDATE" syntax to implement row lock functionality, such as Sybase using the "HOLDLOCK" syntax to implement row locking, so for your database abstraction layer to maintain portability, I still advise you to use Rowlock to implement the row lock function, As for portability, give it to ADODB, well, it's a little far away, and here we are today.

The above is a description of the code demonstration of PHP + MySQL transaction operations, and hopefully will bring you some help in this regard.

Report:

There is a Settransactionmode () method in ADODB that enables you to set the isolation level of the transaction as follows:

Settransactionmode allows you-pass in the transaction mode-to-use for all subsequent transactions for that connection s Ession. Note:if You has persistent connections and using MySQL or MSSQL, you might has to explicitly reset your transaction mod E at the beginning for each page request. This is a supported in PostgreSQL, MSSQL, MySQL with InnoDB and Oci8 currently. For example: $db->settransactionmode ("SERIALIZABLE"); $db->begintrans (); $db->execute (...); $db->execute (...); $db->commitrans (); $db->settransactionmode (""); Restore to default $db->starttrans (); $db->execute (...); $db->execute (...); $db->completetrans (); Supported values to pass in: * READ UNCOMMITTED (allows dirty reads, but fastest) * Read COMMITTED (default Postgres, MSSQ L and OCI8) * repeatable READ (default MySQL) * SERIALIZABLE (slowest and most restrictive)

The above related content is the PHP + MySQL transaction operation code demonstration of the introduction, hope you can gain.

Actual application code Demo for PHP + MySQL transaction operation

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.