>  기사  >  데이터 베이스  >  MySQL에서의 트랜잭션 처리에 대한 자세한 코드 설명

MySQL에서의 트랜잭션 처리에 대한 자세한 코드 설명

零到壹度
零到壹度원래의
2018-04-04 11:41:021154검색


이 글은 주로 MySQL의 트랜잭션 처리에 대한 자세한 설명을 소개합니다. 편집자는 꽤 좋다고 생각해서 지금 공유하고 참고용으로 제공하겠습니다. 에디터를 따라가며 함께 살펴볼까요

단계:

1. 트랜잭션 시작start transaction

트랜잭션을 시작하면 모든 SQL 작업이 메모리에서 발생하지만 실제는 없습니다. 데이터베이스 디스크의 파일에 대한 피드백!

2. 롤백 rollback

롤백은 거래가 시작되기 전의 상태로 복원하는 것입니다!

참고: 롤백 작업을 수행하면 거래가 자동으로 종료됩니다. 거래를 다시 실행하려면 거래를 다시 열어야 합니다.

3 .3 제출 incommitsic transactions의 원칙적으로 실행되는 이유는 즉시 실행되고 적용되는 이유는 기본적으로 mysql이기 때문입니다. SQL 문을 지원합니다. 실행이 자동으로 제출됩니다! 따라서 트랜잭션 열기의 핵심은 이전 자동 제출 기능을 끄고 대신 사용자가 수동으로(commit 문을 사용하여) 제출하는 것입니다!

트랜잭션 단계 요약:

1, 트랜잭션 열기

2, 실행이 성공하면 커밋 제출

3. 실행에 실패한 SQL 문이 있으면 롤백하세요!

가장 일반적인 거래 처리는 돈을 빌리고 갚는 것입니다. 다음은 Zhang San이 Li Si에게 1,000위안을 상환하는 예입니다

먼저 데이터베이스에서 해당 금액을 확인하세요

다음은 상환 거래를 처리하는 코드입니다.


<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'><?<span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;">php 

</span><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);">/**
* MySQL实现事务操作<br/>*/<br/><br/></span><span style="font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);">echo</span> "<meta charset=utf-8>"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 1 连接数据库<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$link</span> = @<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_connect</span>('localhost','root','') or <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>die</span>('连接数据库失败'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_select_db</span>('test',<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$link</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>('set names utf8'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 2  开启事务<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>("start transaction"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//设置一个变量,用来判断所有sql语句是否成功<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>true</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 2.1执行事务中的一组sql语句 <br><br>// 李四的money+1000<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span> = "update pdo set mone=money+1000 where name='李四'"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>(<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>if</span> (!<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>) {   <br>     </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//若sql语句执行失败,把$falg设置为false</span>
    <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>false</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;
}<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//张三的money-1000<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span> = "update pdo set money=money-1000 where name='张三'"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>(<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$sql</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>if</span> (!<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$res</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>) {  <br>     </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//若sql语句执行失败,把$falg设置为false</span>
    <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span> = <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>false</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;
}<br><br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 2.2 判断事务是否执行成功<br></span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>if</span> (<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(128, 0, 128);'>$flag</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>) {   <br>     </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>//所有sql语句执行成功,把sql语句提交</span>
    <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>('commit'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);    </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>echo</span> "还钱成功!"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;
}</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>else</span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>{  <br>     </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 0);'>// 如其中一条执行失败,则回滚到事务开启之前的状态</span>
    <span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 128, 128);'>mysql_query</span>('rollback'<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>);    </span><span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5; color: rgb(0, 0, 255);'>echo</span> "还钱失败!"<span style='font-family: 微软雅黑, "Microsoft YaHei"; font-size: 14px; margin: 0px; padding: 0px; line-height: 1.5;'>;
}</span></span>


Result:

아래에서는 트랜잭션이 정상적으로 처리되는지, 데이터베이스의 금액이 변경되는지 확인하기 위해 의도적으로 필드 중 하나를 잘못 작성했습니다!

<span style='font-size: 14px; font-family: 微软雅黑, "Microsoft YaHei";'>// 李四的money+1000<br>$sql = "update pdo set mone=money+1000 where name='李四'";  //把moeny字段错写成mone<br></span>

결과:

결과는 상환에 실패했고 데이터베이스에 있는 해당 금액은 변경되지 않았습니다. 이는 특정 명령문이 성공적으로 실행되지 않았을 때를 나타냅니다. 트랜잭션은 제출되지 않지만 트랜잭션을 시작하기 전의 데이터를 롤백하고 원래 상태로 복원합니다. 이는 트랜잭션을 사용하는 역할이기도 합니다. 즉, 트랜잭션의 모든 SQL 문이 성공적으로 실행된 경우에만 트랜잭션이 수행됩니다. 그렇지 않으면 롤백됩니다!

위 내용은 MySQL에서의 트랜잭션 처리에 대한 자세한 코드 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.