您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP实例分析了mysql事务处理的使用技巧

2025/8/14 8:33:09发布16次查看
本篇文章主要介绍php实例分析了mysql事务处理的使用技巧,感兴趣的朋友参考下,希望对大家有所帮助。
本文实例讲述了php+mysql基于事务处理实现转账功能的方法,具体如下:
<?php header("content-type:text/html;charset=utf-8"); $mysqli=new mysqli("localhost","root","","test"); if(mysqli_connect_errno()) { printf("连接失败:%s<br>",mysqli_connect_error()); exit(); } $success=true; $price=8000; $result=$mysqli->query("select cash from account where name='usera'"); while($row=$result->fetch_assoc()) { $value=$row["cash"]; echo $value; } $mysqli->autocommit(0); if($value>=$price){ $result=$mysqli->query("update account set cash=cash-$price where name='usera'"); }else { echo '余额不足'; exit(); } if(!$result or $mysqli->affected_rows!=1) { $success=false; } $result=$mysqli->query("update account set cash=cash+$price where name='userb'"); if(!result or $mysqli->affected_rows!=1){ $success=false; } if($success) { $mysqli->commit(); echo '转账成功!'; }else { $mysqli->rollback(); echo "转账失败!"; } $mysqli->autocommit(1); $query="select cash from account where name=?"; $stmt=$mysqli->prepare($query); $stmt->bind_param('s',$name); $name='usera'; $stmt->execute(); $stmt->store_result(); $stmt->bind_result($cash); while($stmt->fetch()) echo "用户usera的值为:".$cash; $mysqli->close();?>
数据库sql语句如下:
create table account{ userid smallint unsigned not null auto_increment, name varchar(45) not null, cash decimal(9,2) not null, primary key(userid))type=innodb;insert into account(name,cash) values ('usera','2000');insert into account(name,cash) values ('userb','10000');
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。
相关推荐:
php 参数和数据过滤图文详解
php中存缓分类数据库缓存
php 文件上传管理系统
以上就是php实例分析了mysql事务处理的使用技巧的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product