Transaction usage examples in PHP_PHP tutorial

WBOY
Release: 2016-07-13 09:52:55
Original
827 people have browsed it

Examples of transaction usage in PHP

This article mainly introduces examples of transaction usage in PHP. This article gives the simplest entry-level example. Friends who need it can refer to it. Next

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

//数据库连接

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

?>

1 2

34 5 6 7 8 9
10
11
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
<🎜>//Database connection<🎜> <🎜>$conn = mysql_connect('localhost', 'root', '');<🎜> <🎜>mysql_select_db('test', $conn);<🎜> <🎜>mysql_query("SET NAMES GBK");<🎜> <🎜> <🎜> <🎜>/*<🎜> <🎜>Tables that support transactions must be of InnoDB type<🎜> <🎜>Can only appear once in a transaction:<🎜> <🎜>mysql_query('START TRANSACTION');//Start transaction<🎜> <🎜>mysql_query(' ROLLBACK ');//Rollback transaction<🎜> <🎜>mysql_query('COMMIT');//Submit transaction<🎜> <🎜> <🎜> <🎜>If multiple rollback transactions occur in a transaction, when the transaction is submitted, only all operations on the database from before the first rollback to after the start of the transaction will be cancelled. All operations after the first rollback to before the transaction is committed will be cancelled. The database operation will still be valid, so generally put the rollback statement only before the commit transaction statement <🎜> <🎜>If a transaction does not have a commit statement, all operations on the database below will be executed from the beginning of the transaction (the execution method returns true or false), but will have no impact on the database. However, when the next transaction statement is executed, the previous transaction will be automatically committed. <🎜> <🎜>*/<🎜> <🎜>mysql_query('START TRANSACTION');<🎜> <🎜>$isBad = 0;<🎜> <🎜> <🎜> <🎜>$ins_testTable1 = "INSERT INTO testtable1(NAME,age)VALUES('first',23)";<🎜> <🎜>if(!mysql_query($ins_testTable1)){<🎜> <🎜>$isBad =1;<🎜> <🎜>}<🎜> <🎜>//The insert statement field name is wrong<🎜> <🎜>$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);<🎜> <🎜>?>
http://www.bkjia.com/PHPjc/1006580.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1006580.htmlTechArticleExamples of transaction usage in PHP This article mainly introduces examples of transaction usage in PHP. This article gives the most A simple entry-level example, friends who need it can refer to it? 1 2 3 4 5 6 7 8 9 10...
Related labels:
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!