Home >Backend Development >PHP Tutorial >Transaction usage examples in PHP, PHP transaction usage examples_PHP tutorial

Transaction usage examples in PHP, PHP transaction usage examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:52:56770browse

Transaction usage examples in PHP, PHP transaction usage examples

<&#63;php
//数据库连接
$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $conn);
mysql_query("SET NAMES GBK");

/*
支持事务的表必须是InnoDB类型
一段事务中只能出现一次:
mysql_query('START TRANSACTION');//开始事务
mysql_query(' ROLLBACK ');//回滚事务
mysql_query('COMMIT');//提交事务

如果一段事务中出现多次回滚事务,则在,提交事务时只将第一次回滚前至开始事务后对数据库的所有操作取消,第一次回滚后至提交事务前所有对数据库操作仍将有效,所以一般将回滚语句仅放在提交事务语句前
如果一段事务无提交语句,则从开始事务时以下的所有对数据库操作虽执行(执行方法返回对错),但对数据库无影响,但是在执行下段开始事务语句时,前段事务自动提交
*/
mysql_query('START TRANSACTION');
$isBad = 0;

$ins_testTable1 = "INSERT INTO testtable1(NAME,age)VALUES('first',23)";
if(!mysql_query($ins_testTable1)){
  $isBad =1;
}
//插入语句字段名有错
$ins_testTable2 = "INSERT INTO testtable1(NAME,ages)VALUES('second','24')";
if(!mysql_query($ins_testTable2)){
  $isBad =1;
}
if($isBad == 1){
  echo $isBad;
  mysql_query('ROLLBACK ');
}
mysql_query('COMMIT');
mysql_close($conn);
&#63;>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1006114.htmlTechArticleTransaction usage examples in PHP, PHP transaction usage examples php//Database connection $conn = mysql_connect('localhost' , 'root', '');mysql_select_db('test', $conn);mysql_query("SET NAMES GBK");/...
Statement:
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