How to write PHP transactions?

Source: Internet
Author: User
How to write PHP transactions? How to write PHP transactions? I am an update and an insert
The code is as follows. the error is that a value type error occurs and the transaction does not work.

$db = new DbManager();$conn = $db->getConnection();$conn->set_charset("utf8");if(mysqli_connect_errno()){echo 'error connect database! please content manager!';exit;}//echo connection_status();$tbNums = explode(',',$tbNum);$conn->autocommit(FALSE);$flag = true;$a=0;foreach($tbNums as $key => $value){$updateBalance = "UPDATE T_SMS_TBNUM SET STATUS = 2 WHERE ID = ?";$insertRelate = "INSERT INTO T_SMS_TBNUMRELATE (TBID,OWNCLIENTID,REALFEE,CREATETIME) VALUES(?,?,?,?)";$stmtUpdateClient = $conn->prepare($updateBalance);$stmtUpdateClient->bind_param('i',$value);$ss = $stmtUpdateClient->execute();$stmt = $conn->prepare($insertRelate);$foura = 4;$a++;if($a!=0){$value = 's';}$stmt->bind_param('iids',$value,$clientId,$foura,date('Y-m-d H:i:s'));$boooo = $stmt->execute();echo $ss.'+'.$boooo;if($stmtUpdateClient->affected_rows <= 0 || $stmt->affected_rows <= 0){echo $stmtUpdateClient->affected_rows.'|'.$stmt->affected_rows;$flag = false;break;}}if(!$flag){$conn ->rollback();$conn -> autocommit(TRUE);$conn->close();return 'paylog false!';}else{$conn->commit();$conn -> autocommit(TRUE);$conn->close();return '1';}


Reply to discussion (solution)

if( $stmt->affected_rows <= 0 ||  ($masterId && $stmtLower->affected_rows <= 0)){$conn ->rollback();$conn -> autocommit(TRUE);$conn->close();return 'unknow_error';}else{$conn->commit();$conn -> autocommit(TRUE);$conn->close();return '1';}
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.