Php+mysqli How to use transaction processing transfer problem _php tutorial

Source: Internet
Author: User

Php+mysqli How to use transaction processing transfer problem


php+mysqli How to use transaction processing transfer problem

In this paper, we describe the method of Php+mysqli using transaction processing transfer problem. Share to everyone for reference

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

Header ("content-type:text/html; Charset=utf-8 ");

$mysqli = new Mysqli ("localhost", "root", "064319", "PHP");

$mysqli->set_charset ("UTF8");

if ($mysqli->connect_errno) {

Die (' Database connection failed '. $mysqli->connect_error);

}

$mysqli->autocommit (FALSE); Auto-commit mode set to False

$flag = true; Flag of whether the transaction was executed successfully

$query = "Update account set balance=balance-1000 where id=3";

$result = $mysqli->query ($query);

$affected _count = $mysqli->affected_rows;

if (!result | | $affected _count = = 0) {//Failed

$flag = false;

}

$query = "Update account set balance=balance+1000 where id=2";

$result = $mysqli->query ($query);

$affected _count = $mysqli->affected_rows;

if (! $result | | $affected _count = = 0) {

$flag = false;

}

if ($flag) {

$mysqli->commit ();

Echo ' transfer success ';

} else {

$mysqli->rollback ();

Echo ' transfer failed ';

}

$mysqli->autocommit (TRUE); Reset transaction to Auto Commit

$mysqli->close ();

?>

Code two:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

try{

$pdo =new PDO ("Mysql:host=localhost;dbname=psp", "Root", "");

$pdo->exec ("Set names UTF8");

$pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);//Set exception handling mode

$pdo->setattribute (pdo::attr_autocommit,0);//Turn off autocommit

}catch (Pdoexception $e) {

echo "Database connection failed";

Exit

}

try{

$age = 10;

$pdo->begintransaction ();//Start transaction

$affected _rows1= $pdo->exec ("Update kfry set k_age=k_age+{$age} where k_name= ' User1 '");

$affected _rows2= $pdo->exec ("Update kfry set k_age=k_age-{$age} where k_name= ' User2 '");//arbitrarily change to make it perform successfully or fail

/* IF ($affected _rows1&& $affected _rows2)

{

$pdo->commit ();

Echo "Operation succeeded";

}else{

$pdo->rollback ();

} */

if (! $affected _rows1)

throw new Pdoexception ("Add error");

if (! $affected _rows2)

throw new Pdoexception ("reduce error");

Echo "Operation succeeded";

$pdo->commit ();//If the execution of the previous two update SQL statement succeeds, the entire transaction executes successfully

}catch (Pdoexception $e) {

Echo "Operation failed:". $e->getmessage ();

$pdo->rollback ();//The statement in the execution transaction is out of the question and the whole transaction is undone

}

$pdo->setattribute (pdo::attr_autocommit,1);

Whether the test was successful

echo "\ nthe operation result is: \ n";

$sql = "SELECT * from Kfry";

$result = $pdo->query ($sql);

foreach ($result as $v)

{

echo $v [' K_name ']. " ". $v [' K_age ']." \ n ";

}

?>

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/988369.html www.bkjia.com true http://www.bkjia.com/PHPjc/988369.html techarticle Php+mysqli the method of using transaction transfer problem Php+mysqli the method of transaction transfer problem in this paper, the Php+mysqli method of transaction transfer problem is described. Share to ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.