Home > Backend Development > PHP Tutorial > php mysql transaction processing implementation program_PHP tutorial

php mysql transaction processing implementation program_PHP tutorial

WBOY
Release: 2016-07-13 10:46:34
Original
853 people have browsed it

The conditions for realizing this function are the environment mysql 5.2/php 5. The table type that supports transactions requires InnoDB. With these conditions, you can implement the above. This transaction rollback operation is often used in large projects. It will be used in banks, e-commerce, etc. Friends in need can refer to it.

Recently, the project software has been upgraded to support transaction processing. I will give you an example for your reference
Environment mysql 5.2/php 5
Table types that support transactions require InnoDB

The code is as follows
 代码如下 复制代码

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

Copy code
mysql_select_db('web_his',$LinkID);

mysql_query("set names utf8");/* Create transaction */ mysql_query('START TRANSACTION') or exit(mysql_error()); $ssql1="insert into pf_item values('22','we','30')"; //Execute sql 1 if(!mysql_query($ssql1)){ echo $ssql1.mysql_errno().":".mysql_error()."
"; Mysql_query('ROLLBACK') or exit(mysql_error());//Determine rollback when execution fails exit; } $ssql1="insert into pf_item values('21','hell','10')"; //Execute sql 2 if(!mysql_query($ssql1)){
echo $ssql1.mysql_errno().":".mysql_error()."
"; Mysql_query('ROLLBACK') or exit(mysql_error());//Determine rollback when execution fails
exit;
} mysql_query('COMMIT') or exit(mysql_error());//Execute transaction mysql_close($LinkID); ?> http://www.bkjia.com/PHPjc/632945.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632945.htmlTechArticleThe conditions for realizing this function are the environment mysql 5.2/php 5. The table type that supports transactions requires InnoDB. With these Under the conditions, you can implement the above. This rollback operation is a big deal...
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