Home > php教程 > php手册 > body text

php mysql 事务处理实现程序

WBOY
Release: 2016-06-13 09:49:17
Original
1102 people have browsed it

要实现本功能的条件是环境 mysql 5.2 /php 5 支持事务的table 类型 需要InnoDB ,有了这些条件你就可以做上面的实现了,这个事物回滚操作是大项目经常用到的,像银行,电子商务等都会用到,有需要的朋友可以参考一下。

近期项目软件升级到支持事务处理,做个示例供大家学习参考
环境 mysql 5.2 /php 5
支持事务的table 类型 需要InnoDB

 代码如下 复制代码

$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);
?>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!