[PHP]-PDO transaction operations

Source: Internet
Author: User

PHP uses the PDO transaction operations database.

Reference article:

Http://php.ncong.com/mysql/pdo/pdo_shiwu.html

On the code:

<!doctype html>PHPTry {            $pdo=NewPDO ("Mysql:host=localhost;dbname=test", "root", "XXXXXX"); //set the mode of error usage exception            $pdo-SetAttribute (Pdo::attr_errmode, PDO::errmode_exception); //Turn off auto-commit            $pdoSetAttribute (pdo::attr_autocommit, 0); } Catch(pdoexception$e) {            Echo sprintf("Exception message=%s",$e-getMessage ()); Exit(); }                $STRSQL= "UPDATE mytable SET headline= ' haha ' WHERE id=:id"; $pdo-BeginTransaction (); Try {             for($i= 1;$i<= 5;$i++) {                $stmt=$pdo->prepare ($STRSQL); $affectedRows=$stmt->execute (Array("id" =$i)); if($affectedRows> 0) {                    Echo sprintf("Update Success id=%s<br/>",$i); }                Else {                    Echo sprintf("Update false id=%s",$i); }            }            $pdo-commit (); } Catch(pdoexception$e) {            Echo sprintf("Exception message=%s",$e-getMessage ()); $pdo-RollBack (); Exit(); }                //need to turn on auto-commit        $pdo->setattribute (Pdo::attr_autocommit, 1); Echo"; $stm=$pdo->prepare ("Select ID, Headline, create_time from MyTable"); $stm-execute (); $stm->bindcolumn (1,$id); $stm->bindcolumn ("headline",$headline); $stm->bindcolumn (3,$createTime);  while($stm->fetch (PDO::fetch_bound)) {            Echo"<p>",$id, " - " ,$headline, " - ",$createTime, "</p>"; }    ?> </body>

[PHP]-PDO transaction operations

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.