{"id":3783,"date":"2025-06-28T12:56:13","date_gmt":"2025-06-28T04:56:13","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3783"},"modified":"2025-06-28T12:56:13","modified_gmt":"2025-06-28T04:56:13","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e4%ba%a4%e6%98%93%e6%9d%a5%e7%a1%ae%e4%bf%9dphp%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e4%b8%80%e8%87%b4%e6%80%a7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/06\/28\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e4%ba%a4%e6%98%93%e6%9d%a5%e7%a1%ae%e4%bf%9dphp%e4%b8%ad%e7%9a%84%e6%95%b0%e6%8d%ae%e4%b8%80%e8%87%b4%e6%80%a7%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528\u4ea4\u6613\u6765\u786e\u4fddPHP\u4e2d\u7684\u6570\u636e\u4e00\u81f4\u6027\uff1f"},"content":{"rendered":"\n<p>\u5728 PHP \u4e2d\uff0c\u4e3a\u4e86\u786e\u4fdd<strong>\u6570\u636e\u4e00\u81f4\u6027<\/strong>\uff08\u5c24\u5176\u5728\u591a\u6b65\u5199\u5165\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u4e2d\uff09\uff0c\u6211\u4eec\u4f7f\u7528 <strong>\u6570\u636e\u5e93\u4e8b\u52a1\uff08transaction\uff09<\/strong>\u3002\u4e8b\u52a1\u53ef\u4ee5\u4fdd\u8bc1<strong>\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25<\/strong>\uff0c\u9632\u6b62\u56e0\u90e8\u5206\u6267\u884c\u6210\u529f\u800c\u5bfc\u81f4\u6570\u636e\u5f02\u5e38\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"> \u4e00\u3001\u4ec0\u4e48\u662f\u4e8b\u52a1\uff08Transaction\uff09<\/h2>\n\n\n\n<p>\u4e8b\u52a1\u662f\u4e00\u7ec4 SQL \u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u5168\u90e8\u6267\u884c\uff0c\u8981\u4e48\u4e00\u4e2a\u4e5f\u4e0d\u6267\u884c\u3002\u4e8b\u52a1\u5177\u6709\u56db\u5927\u7279\u6027\uff0c\u7b80\u79f0 <strong>ACID<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7279\u6027<\/th><th>\u542b\u4e49<\/th><\/tr><\/thead><tbody><tr><td>\u539f\u5b50\u6027\uff08Atomicity\uff09<\/td><td>\u64cd\u4f5c\u4e0d\u53ef\u5206\u5272\uff0c\u5168\u90e8\u6210\u529f\u6216\u5168\u90e8\u5931\u8d25<\/td><\/tr><tr><td>\u4e00\u81f4\u6027\uff08Consistency\uff09<\/td><td>\u6267\u884c\u524d\u540e\u6570\u636e\u6ee1\u8db3\u4e00\u81f4\u6027\u89c4\u5219<\/td><\/tr><tr><td>\u9694\u79bb\u6027\uff08Isolation\uff09<\/td><td>\u591a\u4e2a\u4e8b\u52a1\u4e4b\u95f4\u4e92\u4e0d\u5e72\u6270<\/td><\/tr><tr><td>\u6301\u4e45\u6027\uff08Durability\uff09<\/td><td>\u63d0\u4ea4\u540e\u7ed3\u679c\u6c38\u4e45\u4fdd\u5b58<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5728 PHP \u4e2d\u4f7f\u7528\u4e8b\u52a1\u7684\u524d\u63d0\u6761\u4ef6<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5fc5\u987b\u4f7f\u7528\u652f\u6301\u4e8b\u52a1\u7684\u6570\u636e\u5e93\u5f15\u64ce<\/strong>\uff0c\u5982 MySQL \u7684 InnoDB\uff08\u274c MyISAM \u4e0d\u652f\u6301\u4e8b\u52a1\uff09\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u4f7f\u7528 PDO \u6267\u884c\u4e8b\u52a1\uff08\u63a8\u8350\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u793a\u4f8b\uff1a\u8f6c\u8d26\u64cd\u4f5c\uff08A \u2192 B\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$pdo = new PDO(\"mysql:host=localhost;dbname=test\", \"root\", \"\");\n\n\/\/ \u8bbe\u7f6e\u4e3a\u629b\u51fa\u5f02\u5e38\n$pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\ntry {\n    \/\/ \u5f00\u542f\u4e8b\u52a1\n    $pdo-&gt;beginTransaction();\n\n    \/\/ \u7528\u6237 A \u6263\u6b3e\n    $stmt1 = $pdo-&gt;prepare(\"UPDATE users SET balance = balance - :amount WHERE id = :from\");\n    $stmt1-&gt;execute(&#91;':amount' =&gt; 100, ':from' =&gt; 1]);\n\n    \/\/ \u7528\u6237 B \u52a0\u6b3e\n    $stmt2 = $pdo-&gt;prepare(\"UPDATE users SET balance = balance + :amount WHERE id = :to\");\n    $stmt2-&gt;execute(&#91;':amount' =&gt; 100, ':to' =&gt; 2]);\n\n    \/\/ \u63d0\u4ea4\u4e8b\u52a1\n    $pdo-&gt;commit();\n    echo \"\u8f6c\u8d26\u6210\u529f\uff01\";\n\n} catch (Exception $e) {\n    \/\/ \u56de\u6eda\u4e8b\u52a1\n    $pdo-&gt;rollBack();\n    echo \"\u8f6c\u8d26\u5931\u8d25\uff1a\" . $e-&gt;getMessage();\n}<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u4e00\u65e6\u4efb\u610f SQL \u62a5\u9519\uff08\u6bd4\u5982\u4f59\u989d\u4e0d\u8db3\u6216\u8bed\u6cd5\u9519\u8bef\uff09\uff0c\u4e8b\u52a1\u4f1a\u56de\u6eda\uff0c\u4fdd\u8bc1\u4e0d\u4f1a\u53d1\u751f\u201c\u6263\u4e86 A \u6ca1\u52a0\u5230 B\u201d\u7684\u6570\u636e\u9519\u4e71\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u4e8b\u52a1\u5931\u6548\u7684\u5e38\u89c1\u539f\u56e0<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u539f\u56e0<\/th><th>\u63cf\u8ff0<\/th><\/tr><\/thead><tbody><tr><td>\u4f7f\u7528\u4e86 MyISAM \u8868<\/td><td>\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u9700\u6539\u4e3a InnoDB<\/td><\/tr><tr><td>\u6ca1\u542f\u7528\u5f02\u5e38\u5904\u7406<\/td><td>PDO \u9ed8\u8ba4\u4e0d\u629b\u5f02\u5e38\uff0c\u9700\u624b\u52a8\u8bbe\u7f6e <code>ERRMODE_EXCEPTION<\/code><\/td><\/tr><tr><td>\u90e8\u5206\u8bed\u53e5\u4e0d\u5728\u4e8b\u52a1\u4e2d<\/td><td>\u6bd4\u5982\u67d0\u4e9b\u5199\u64cd\u4f5c\u5728\u4e8b\u52a1\u5f00\u59cb\u4e4b\u5916<\/td><\/tr><tr><td>\u5199\u64cd\u4f5c\u81ea\u52a8\u63d0\u4ea4<\/td><td>\u5982 autocommit \u6ca1\u5173\u95ed\u6216\u5199\u5728\u975e\u4e8b\u52a1\u5185<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u4f7f\u7528 mysqli \u4e8b\u52a1\uff08\u8f83\u4f4e\u7ea7\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$conn = new mysqli(\"localhost\", \"root\", \"\", \"test\");\n$conn-&gt;autocommit(false); \/\/ \u5173\u95ed\u81ea\u52a8\u63d0\u4ea4\n\ntry {\n    $conn-&gt;query(\"UPDATE users SET balance = balance - 100 WHERE id = 1\");\n    $conn-&gt;query(\"UPDATE users SET balance = balance + 100 WHERE id = 2\");\n\n    $conn-&gt;commit();\n    echo \"\u8f6c\u8d26\u6210\u529f\";\n} catch (Exception $e) {\n    $conn-&gt;rollback();\n    echo \"\u8f6c\u8d26\u5931\u8d25\uff1a\" . $e-&gt;getMessage();\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5b9e\u6218\u5efa\u8bae<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u573a\u666f<\/th><th>\u662f\u5426\u5efa\u8bae\u7528\u4e8b\u52a1<\/th><\/tr><\/thead><tbody><tr><td>\u7528\u6237\u6ce8\u518c\uff08\u591a\u8868\uff09<\/td><td>\u2705 \u662f<\/td><\/tr><tr><td>\u7528\u6237\u53d1\u5e16\uff08\u5e16\u5b50 + \u9644\u4ef6\uff09<\/td><td>\u2705 \u662f<\/td><\/tr><tr><td>\u7535\u5546\u4e0b\u5355\uff08\u8ba2\u5355\u3001\u5e93\u5b58\u3001\u652f\u4ed8\uff09<\/td><td>\u2705 \u5fc5\u987b<\/td><\/tr><tr><td>\u4ec5\u67e5\u8be2\u64cd\u4f5c<\/td><td>\u274c \u4e0d\u5efa\u8bae\uff08\u65e0\u610f\u4e49\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5b89\u5168\u4e0e\u6027\u80fd\u63d0\u793a<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5c3d\u91cf\u63a7\u5236\u4e8b\u52a1\u65f6\u95f4\uff08\u907f\u514d\u4e8b\u52a1\u671f\u95f4\u957f\u65f6\u95f4\u7b49\u5f85\u7528\u6237\u8f93\u5165\uff09<\/li>\n\n\n\n<li>\u907f\u514d\u5728\u4e8b\u52a1\u4e2d\u6267\u884c\u8fc7\u591a\u975e\u5fc5\u8981\u903b\u8f91<\/li>\n\n\n\n<li>\u4f7f\u7528\u6b7b\u9501\u5904\u7406\u903b\u8f91\uff08\u5982\u6355\u83b7\u540e\u91cd\u8bd5\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u516b\u3001\u6743\u5a01\u53c2\u8003\u8d44\u6599<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/pdo.begintransaction.php\">\ud83d\udcd8 PHP PDO::beginTransaction &#8211; \u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/innodb-transactions.html\">\ud83d\udcd8 MySQL InnoDB \u4e8b\u52a1\u652f\u6301\u8bf4\u660e<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/owasp.org\/www-project-top-ten\/\">\ud83d\udcd8 OWASP &#8211; \u6570\u636e\u4e00\u81f4\u6027\u4e0e\u4e8b\u52a1\u5904\u7406\u5b89\u5168\u5efa\u8bae<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\uff1a\u4e8b\u52a1\u4f7f\u7528\u6d41\u7a0b\uff08PDO\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    $pdo-&gt;beginTransaction();\n\n    \/\/ \u5199\u64cd\u4f5c 1\n    \/\/ \u5199\u64cd\u4f5c 2\n\n    $pdo-&gt;commit();  \/\/ \u4e00\u8d77\u6210\u529f\n} catch (Exception $e) {\n    $pdo-&gt;rollBack();  \/\/ \u4efb\u4e00\u5931\u8d25\uff0c\u5168\u4f53\u5931\u8d25\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\uff0c\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\uff08\u5c24\u5176\u5728\u591a\u6b65\u5199\u5165\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u4e2d\uff09\uff0c\u6211\u4eec\u4f7f\u7528 \u6570\u636e\u5e93\u4e8b\u52a1\uff08transactio [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3784,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-3783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3783"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3783\/revisions"}],"predecessor-version":[{"id":3785,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3783\/revisions\/3785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3784"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}