php实现mysql事务处理的方法
作者:shichen2014 时间:2023-11-10 10:42:39
本文实例讲述了php实现mysql事务处理的方法。分享给大家供大家参考。具体分析如下:
要实现本功能的条件是环境 mysql 5.2 /php 5 支持事务的table 类型,需要InnoDB,有了这些条件你就可以做上面的实现了,这个事物回滚操作是大项目经常用到的,像银行,电子商务等都会用到,有需要的朋友可以参考一下.
近期项目软件升级到支持事务处理,做个示例供大家学习参考.
环境 mysql 5.2 /php 5
支持事务的table 类型 需要InnoDB
php mysql 事务处理实现程序代码如下:
<?PHP
$LinkID =mysql_connect('localhost:3307','root',*******);
mysql_select_db('web_his',$LinkID);
mysql_query("set names utf8");
/* 创建事务 */
mysql_query('START TRANSACTION') or exit(mysql_error());
$ssql1="insert into pf_item values('22','我们','30')"; //执行sql 1
if(!mysql_query($ssql1)){
echo $ssql1.mysql_errno().":".mysql_error()."<br>";
mysql_query('ROLLBACK') or exit(mysql_error());//判断当执行失败时回滚
exit;
}
$ssql1="insert into pf_item values('21','hell','10')"; //执行sql 2
if(!mysql_query($ssql1)){
echo $ssql1.mysql_errno().":".mysql_error()."<br>";
mysql_query('ROLLBACK') or exit(mysql_error());//判断当执行失败时回滚
exit;
}
mysql_query('COMMIT') or exit(mysql_error());//执行事务
mysql_close($LinkID);
?>
希望本文所述对大家的php程序设计有所帮助。
标签:php,mysql,事务
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Echarts基本入门之柱状图、折线图通用配置
2024-04-28 09:37:10
![](https://img.aspxhome.com/file/2023/6/132856_0s.png)
python爬虫教程之bs4解析和xpath解析详解
2023-09-22 19:43:06
vue 页面卡死,点击无反应的问题及解决
2024-04-27 16:04:21
![](https://img.aspxhome.com/file/2023/7/133017_0s.png)
Django模板继承与模板的导入实例详解
2023-04-03 14:41:28
![](https://img.aspxhome.com/file/2023/3/92143_0s.gif)
pandas数据分组groupby()和统计函数agg()的使用
2023-09-07 22:25:46
![](https://img.aspxhome.com/file/2023/2/102332_0s.png)
Pandas中resample方法详解
2021-10-03 09:57:40
深入理解Python中的super()方法
2022-11-07 05:10:45
Dreamweaver快捷键大全
2007-11-05 14:08:00
通过python读取txt文件和绘制柱形图的实现代码
2023-11-23 11:24:43
![](https://img.aspxhome.com/file/2023/1/97331_0s.png)
Dialog 按照顺序弹窗的优雅写法
2021-10-18 18:35:25
Python ORM框架Peewee用法详解
2022-11-08 03:00:24
![](https://img.aspxhome.com/file/2023/1/101101_0s.png)
完美解决torch.cuda.is_available()一直返回False的玄学方法
2023-04-05 18:17:34
![](https://img.aspxhome.com/file/2023/8/128218_0s.jpg)
数据分页显示按钮,自动获取url链接
2009-06-22 12:59:00
python实现人脸识别代码
2022-12-20 15:39:06
![](https://img.aspxhome.com/file/2023/8/76858_0s.jpg)
在keras下实现多个模型的融合方式
2023-06-03 17:14:59
![](https://img.aspxhome.com/file/2023/5/77755_0s.jpg)
Python 中面向接口编程详情
2022-05-06 19:55:41
六个窍门助你提高Python运行效率
2021-07-19 08:31:15
python列表推导式入门学习解析
2022-04-25 14:28:00
使用 Python 处理3万多条数据只要几秒钟
2023-08-03 15:21:06
![](https://img.aspxhome.com/file/2023/6/60296_0s.jpg)
CSS网页布局扩展小技巧
2010-06-03 12:13:00