国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP+Mysql基于事務處理實現轉賬功能的方法

2024-05-04 23:37:29
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP+Mysql基于事務處理實現轉賬功能的方法,實例分析了mysql事務處理的使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP+Mysql基于事務處理實現轉賬功能的方法。分享給大家供大家參考。具體如下:

 

 
  1. <?php 
  2. header("Content-Type:text/html;charset=utf-8"); 
  3. $mysqli=new mysqli("localhost","root","","test"); 
  4. if(mysqli_connect_errno()) 
  5. printf("連接失敗:%s<br>",mysqli_connect_error()); 
  6. exit(); 
  7. $success=TRUE; 
  8. $price=8000; 
  9. $result=$mysqli->query("select cash from account where name='userA'"); 
  10. while($row=$result->fetch_assoc()) 
  11. $value=$row["cash"]; 
  12. echo $value
  13. $mysqli->autocommit(0); 
  14. if($value>=$price){ 
  15. $result=$mysqli->query("UPDATE account set cash=cash-$price where name='userA'"); 
  16. }else { 
  17. echo '余額不足'
  18. exit(); 
  19. if(!$result or $mysqli->affected_rows!=1) 
  20. $success=FALSE; 
  21. $result=$mysqli->query("UPDATE account set cash=cash+$price where name='userB'"); 
  22. if(!result or $mysqli->affected_rows!=1){ 
  23. $success=FALSE; 
  24. if($success
  25. $mysqli->commit(); 
  26. echo '轉賬成功!'
  27. }else 
  28. $mysqli->rollback(); 
  29. echo "轉賬失敗!"
  30. $mysqli->autocommit(1); 
  31. $query="select cash from account where name=?"
  32. $stmt=$mysqli->prepare($query); 
  33. $stmt->bind_param('s',$name); 
  34. $name='userA'
  35. $stmt->execute(); 
  36. $stmt->store_result(); 
  37. $stmt->bind_result($cash); 
  38. while($stmt->fetch()) 
  39. echo "用戶userA的值為:".$cash
  40. $mysqli->close(); 
  41. ?> 

數據庫SQL語句如下:

 

 
  1. create table account{ 
  2. userID smallint unsigned not null auto_increment, 
  3. name varchar(45) not null, 
  4. cash decimal(9,2) not null, 
  5. primary key(userID) 
  6. )type=InnoDB; 
  7. insert into account(name,cash) values ('userA','2000'); 
  8. insert into account(name,cash) values ('userB','10000'); 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江安县| 井陉县| 巍山| 开封县| 咸阳市| 乌恰县| 灵宝市| 大邑县| 怀来县| 衡阳市| 堆龙德庆县| 大新县| 紫阳县| 古交市| 汉沽区| 新巴尔虎左旗| 龙川县| 南城县| 海林市| 霸州市| 璧山县| 青川县| 博乐市| 双柏县| 新巴尔虎左旗| 楚雄市| 镇坪县| 甘泉县| 彰化市| 浮山县| 林甸县| 嘉祥县| 威信县| 砚山县| 宕昌县| 县级市| 舒兰市| 苍溪县| 石渠县| 高邑县| 阿拉善盟|