php实现mysql事务处理的方法,phpmysql事务处理
本文实例讲述了php实现mysql事务处理的方法。分享给大家供大家参考。具体分析如下:
要实现本功能的条件是环境 mysql 5.2 /php 5 支持事务的table 类型,需要InnoDB,有了这些条件你就可以做上面的实现了,这个事物回滚操作是大项目经常用到的,像银行,电子商务等都会用到,有需要的朋友可以参考一下.
近期项目软件升级到支持事务处理,做个示例供大家学习参考.
环境 mysql 5.2 /php 5
支持事务的table 类型 需要InnoDB
php mysql 事务处理实现程序代码如下:
复制代码 代码如下:
$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()."
";
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()."
";
mysql_query('ROLLBACK') or exit(mysql_error());//判断当执行失败时回滚
exit;
}
mysql_query('COMMIT') or exit(mysql_error());//执行事务
mysql_close($LinkID);
?>
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor
