PHP--PDO transaction processing

Source: Internet
Author: User

Transaction processing Flow

    1. Open transaction

Pdo::begintransaction

    1. Transactional operations

All the practical operation is to delete and change

    1. Transaction commit

Pdo::commit: Submit data after success

Pdo::rollback: Rollback data after failure

<?PHP//PDO Transaction processing//connection authentication    $pdo=NewPDO (' Mysql:host=localhost;dbname=project ', ' root ', ' root '); //1. Turn on the transaction    $res=$pdo-BeginTransaction (); //Var_dump ($res);    2. Transaction processing (multiple SQL statement executions)    $sql= "Update pro_student set s_age = s_id = 20"; $lines=$pdo-exec($sql); //Var_dump ($lines);    $sql= "SELECT * from pro_student where s_id = 20"; $stmt=$pdo->query ($sql); //Var_dump ($stmt->fetch (PDO::FETCH_ASSOC));    3. Commit a transaction    if($lines){        //Update Successful        $pdo-commit (); }Else{        //Update failed        EchoFailed; $pdo-rollback (); }

Attention

    1. To implement a transaction, the storage engine for the data table must be InnoDB
    2. Transaction must be complete (open à submit)

PHP--PDO transaction processing

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.