Php + mysql transaction processing steps: 1. disable automatic submission 2. start transaction processing 3. if an exception exists, the system automatically throws an exception and prompts you to roll back. 4. note: Only the InnoDB driver in mysql supports transaction processing. the default MyI... several steps for php + mysql transaction processing:
1. disable automatic commit. 2. enable transaction processing. 3. if an exception exists, an exception is automatically thrown, prompting you to roll back. 4. enable automatic commit.
Note: Only the InnoDB driver in mysql supports transaction processing. the MyIsAM driver does not support transaction processing by default. The following is the instance code:
0); // The last step is to disable automatic submission. // $ pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 0 ); // This is done by setting the attribute method to disable automatic submission. the preceding function is the same as $ pdo-> setAttribute (PDO: ATTR_ERRMODE, PDO: ERRMODE_EXCEPTION ); // enable exception handling} catch (PDOException $ e) {echo "database connection failed :". $ e-> getMessage (); exit; // open-source code phprm.com}/** transaction processing ** Zhang San bought a 2000 yuan computer from Li Si * deducted 2000 yuan from Zhang San's account * added 2000 yuan to Li Si's account * remove a computer from the product table * MyIsAM InnoDB */try {$ pdo-> beginTransaction (); // enable transaction processing $ price = 500; $ s Ql = "update zhanghao set price = price-{$ price} where id = 1"; $ affected_rows = $ pdo-> exec ($ SQL); if (! $ Affected_rows) throw new PDOException ("Michael Jacob transfer failed "); // The 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 ("failed to transfer to Li Si"); echo "transaction successful! "; $ Pdo-> commit (); // commit upon successful transaction} catch (PDOException $ e) {echo $ e-> getMessage (); $ pdo-> rollback () ;}$ pdo-> setAttribute (PDO: ATTR_AUTOCOMMIT, 1); // automatic submission. if not, transfer failed // Set error report mode ERRMODE_SILENT ERRMODE_WARNING
Address:
Reprinted at will, but please attach the article address :-)