{"id":96634,"date":"2024-10-20T11:05:17","date_gmt":"2024-10-20T03:05:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96634\/"},"modified":"2024-10-20T11:05:17","modified_gmt":"2024-10-20T03:05:17","slug":"php%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%8b%e5%8b%99%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7%e5%8f%8a%e5%af%a6%e7%8f%be%e6%96%b9%e6%b3%95-php%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%8b%e5%8b%99","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96634\/","title":{"rendered":"php\u6578\u64da\u5eab\u4e8b\u52d9\u7684\u91cd\u8981\u6027\u53ca\u5be6\u73fe\u65b9\u6cd5 (php\u6578\u64da\u5eab\u4e8b\u52d9)"},"content":{"rendered":"<h1 id=\"php%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%8b%e5%8b%99%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7%e5%8f%8a%e5%af%a6%e7%8f%be%e6%96%b9%e6%b3%95-OLTyMvthxr\">PHP\u6578\u64da\u5eab\u4e8b\u52d9\u7684\u91cd\u8981\u6027\u53ca\u5be6\u73fe\u65b9\u6cd5<\/h1>\n<p>\u5728\u73fe\u4ee3\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002\u5c24\u5176\u662f\u5728\u4f7f\u7528PHP\u9032\u884c\u958b\u767c\u6642\uff0c\u6578\u64da\u5eab\u4e8b\u52d9\u7684\u6982\u5ff5\u66f4\u662f\u4e0d\u53ef\u5ffd\u8996\u3002\u672c\u6587\u5c07\u63a2\u8a0ePHP\u6578\u64da\u5eab\u4e8b\u52d9\u7684\u91cd\u8981\u6027\u53ca\u5176\u5be6\u73fe\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u548c\u61c9\u7528\u9019\u4e00\u6280\u8853\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%8b%e5%8b%99%ef%bc%9f-OLTyMvthxr\">\u4ec0\u9ebc\u662f\u6578\u64da\u5eab\u4e8b\u52d9\uff1f<\/h2>\n<p>\u6578\u64da\u5eab\u4e8b\u52d9\u662f\u4e00\u7d44\u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u9019\u4e9b\u64cd\u4f5c\u8981\u9ebc\u5168\u90e8\u6210\u529f\uff0c\u8981\u9ebc\u5168\u90e8\u5931\u6557\u3002\u9019\u4e00\u7279\u6027\u88ab\u7a31\u70ba\u539f\u5b50\u6027\uff08Atomicity\uff09\u3002\u4e8b\u52d9\u901a\u5e38\u9075\u5faaACID\u539f\u5247\uff0c\u5373\u539f\u5b50\u6027\u3001\u4e00\u81f4\u6027\u3001\u9694\u96e2\u6027\u548c\u6301\u4e45\u6027\u3002\u9019\u4e9b\u539f\u5247\u78ba\u4fdd\u4e86\u6578\u64da\u7684\u5b8c\u6574\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%8b%e5%8b%99%e5%a6%82%e6%ad%a4%e9%87%8d%e8%a6%81%ef%bc%9f-OLTyMvthxr\">\u70ba\u4ec0\u9ebc\u6578\u64da\u5eab\u4e8b\u52d9\u5982\u6b64\u91cd\u8981\uff1f<\/h2>\n<ul>\n<li><strong>\u6578\u64da\u5b8c\u6574\u6027\uff1a<\/strong>\u4e8b\u52d9\u78ba\u4fdd\u4e86\u6578\u64da\u5728\u591a\u500b\u64cd\u4f5c\u4e4b\u9593\u7684\u4e00\u81f4\u6027\u3002\u4f8b\u5982\uff0c\u5728\u9280\u884c\u8f49\u8cec\u64cd\u4f5c\u4e2d\uff0c\u5f9e\u4e00\u500b\u8cec\u6236\u6263\u6b3e\u548c\u5411\u53e6\u4e00\u500b\u8cec\u6236\u5b58\u6b3e\u5fc5\u9808\u540c\u6642\u6210\u529f\uff0c\u5426\u5247\u5c07\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\u3002<\/li>\n<li><strong>\u932f\u8aa4\u8655\u7406\uff1a<\/strong>\u5982\u679c\u5728\u4e8b\u52d9\u57f7\u884c\u904e\u7a0b\u4e2d\u767c\u751f\u932f\u8aa4\uff0c\u4e8b\u52d9\u53ef\u4ee5\u56de\u6efe\u5230\u4e4b\u524d\u7684\u72c0\u614b\uff0c\u907f\u514d\u6578\u64da\u640d\u58de\u3002<\/li>\n<li><strong>\u4e26\u767c\u63a7\u5236\uff1a<\/strong>\u4e8b\u52d9\u53ef\u4ee5\u5e6b\u52a9\u7ba1\u7406\u591a\u500b\u7528\u6236\u540c\u6642\u8a2a\u554f\u6578\u64da\u7684\u60c5\u6cc1\uff0c\u78ba\u4fdd\u6578\u64da\u4e0d\u6703\u56e0\u70ba\u4e26\u767c\u64cd\u4f5c\u800c\u51fa\u73fe\u932f\u8aa4\u3002<\/li>\n<\/ul>\n<h2 id=\"php%e4%b8%ad%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e4%ba%8b%e5%8b%99%e7%9a%84%e6%96%b9%e6%b3%95-OLTyMvthxr\">PHP\u4e2d\u5be6\u73fe\u6578\u64da\u5eab\u4e8b\u52d9\u7684\u65b9\u6cd5<\/h2>\n<p>\u5728PHP\u4e2d\uff0c\u5be6\u73fe\u6578\u64da\u5eab\u4e8b\u52d9\u901a\u5e38\u6d89\u53ca\u5230PDO\uff08PHP Data Objects\uff09\u6216MySQLi\u64f4\u5c55\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u9032\u884c\u4e8b\u52d9\u8655\u7406\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-OLTyMvthxr\">1. \u9023\u63a5\u6578\u64da\u5eab<\/h3>\n<pre><code>\n$dsn = 'mysql:host=localhost;dbname=testdb';\n$username = 'root';\n$password = 'password';\n\ntry {\n    $pdo = new PDO($dsn, $username, $password);\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n} catch (PDOException $e) {\n    echo 'Connection failed: ' . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<h3 id=\"2-%e9%96%8b%e5%a7%8b%e4%ba%8b%e5%8b%99-OLTyMvthxr\">2. \u958b\u59cb\u4e8b\u52d9<\/h3>\n<pre><code>\n$pdo-&gt;beginTransaction();\n<\/code><\/pre>\n<h3 id=\"3-%e5%9f%b7%e8%a1%8csql%e6%93%8d%e4%bd%9c-OLTyMvthxr\">3. \u57f7\u884cSQL\u64cd\u4f5c<\/h3>\n<pre><code>\ntry {\n    $pdo-&gt;exec(\"INSERT INTO accounts (name, balance) VALUES ('Alice', 1000)\");\n    $pdo-&gt;exec(\"INSERT INTO accounts (name, balance) VALUES ('Bob', 500)\");\n    \n    \/\/ \u5047\u8a2d\u9019\u88e1\u6709\u4e00\u500b\u932f\u8aa4\n    \/\/ $pdo-&gt;exec(\"INSERT INTO accounts (name, balance) VALUES ('Charlie', NULL)\"); \/\/ \u9019\u5c07\u5f15\u767c\u932f\u8aa4\n\n    $pdo-&gt;commit(); \/\/ \u63d0\u4ea4\u4e8b\u52d9\n} catch (Exception $e) {\n    $pdo-&gt;rollBack(); \/\/ \u56de\u6efe\u4e8b\u52d9\n    echo \"Failed: \" . $e-&gt;getMessage();\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b5%90%e8%ab%96-OLTyMvthxr\">\u7d50\u8ad6<\/h2>\n<p>PHP\u6578\u64da\u5eab\u4e8b\u52d9\u5728\u78ba\u4fdd\u6578\u64da\u5b8c\u6574\u6027\u548c\u53ef\u9760\u6027\u65b9\u9762\u626e\u6f14\u8457\u91cd\u8981\u89d2\u8272\u3002\u901a\u904e\u6b63\u78ba\u5730\u4f7f\u7528\u4e8b\u52d9\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u64cd\u4f5c\uff0c\u907f\u514d\u56e0\u932f\u8aa4\u6216\u4e26\u767c\u554f\u984c\u800c\u5c0e\u81f4\u7684\u6578\u64da\u640d\u58de\u3002\u7121\u8ad6\u662f\u5728\u958b\u767c\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\u6642\uff0c\u7406\u89e3\u548c\u5be6\u73fe\u6578\u64da\u5eab\u4e8b\u52d9\u90fd\u662f\u4e00\u9805\u5fc5\u4e0d\u53ef\u5c11\u7684\u6280\u80fd\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u95dc\u65bc<code>VPS<\/code>\u3001<code>\u9999\u6e2fVPS<\/code>\u3001<code>\u670d\u52a1\u5668<\/code>\u3001<code>\u4e91\u670d\u52a1\u5668<\/code>\u7b49\u76f8\u95dc\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9 <a href=\"https:\/\/server.hk\">Server.HK<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3PHP\u6578\u64da\u5eab\u4e8b\u52d9\u7684\u91cd\u8981\u6027\u53ca\u5176\u5be6\u73fe\u65b9\u6cd5\uff0c\u78ba\u4fdd\u6578\u64da\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\uff0c\u63d0\u5347\u61c9\u7528\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96634","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96634"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96634\/revisions"}],"predecessor-version":[{"id":96635,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96634\/revisions\/96635"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}