Home  >  Article  >  Backend Development  >  Example of mysql transaction processing usage of pdo under php, pdomysql_PHP tutorial

Example of mysql transaction processing usage of pdo under php, pdomysql_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:45771browse

Mysql transaction usage example of pdo under php, pdomysql

The example in this article describes the usage of mysql transaction processing of pdo under php. Share it with everyone for your reference. The specific analysis is as follows:

Several steps of php+mysql transaction processing:

1. Turn off automatic submission 2. Enable transaction processing 3. Automatically throw an exception prompt and roll back if there is an exception 4. Turn on automatic submission

Note: MySQL only supports transaction processing with this InnoDB driver. The default MyIsAM driver does not support it. Here is the example code:

Copy code The code is as follows:
Try{
           $pdo=new pdo("mysql:host=localhost;dbname=mydb", "root", "root", array(PDO::ATTR_AUTOCOMMIT=>0));//Finally, turn off automatic submission
//$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 0);//This is to turn off automatic submission by setting the attribute method. The function is the same as above
           $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Turn on exception handling
}catch(PDOException $e){
echo "Database connection failed:".$e->getMessage();
exit;
}
/*
* Transaction processing
* *
* * Zhang San bought a 2,000 yuan computer from Li Si
* * Deduct 2,000 yuan from Zhang San’s account
* * Add 2,000 yuan to Li Si’s account
* * Remove one computer from the product list
* * MyIsAM InnoDB
*/
Try{
           $pdo->beginTransaction();//Start transaction processing                                                                                       $price=500;
           $sql="update zhanghao set price=price-{$price} where id=1";
           $affected_rows=$pdo->exec($sql);
          if(!$affected_rows)
                 throw new PDOException("Zhang San's transfer failed");//That error throws an exception
          $sql="update zhanghao set price=price+{$price} where id=3";
          $affected_rows=$pdo->exec($sql);                                                                 if(!$affected_rows)
                 throw new PDOException("Transfer to Li Si failed");
echo "Transaction successful!";
$pdo->commit();//Submit if the transaction is successful
}catch(PDOException $e){
echo $e->getMessage();
$pdo->rollback();
}  
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, 1);//Automatic submission, if it is not automatically submitted in the end, the transfer will be unsuccessful
//Set error reporting mode ERRMODE_SILENT ERRMODE_WARNING
?>

I hope this article will be helpful to everyone’s PHP programming design.

http://www.bkjia.com/PHPjc/933597.html

truehttp: //www.bkjia.com/PHPjc/933597.htmlTechArticleExamples of mysql transaction processing usage of pdo under php, pdomysql This article describes the usage of mysql transaction processing of pdo under php. Share it with everyone for your reference. The specific analysis is as follows: php+mysql transaction...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn