{"id":22285,"date":"2024-11-21T12:38:51","date_gmt":"2024-11-21T04:38:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22285\/"},"modified":"2024-11-21T12:38:51","modified_gmt":"2024-11-21T04:38:51","slug":"thinkphp%e5%a6%82%e4%bd%95%e5%81%9a%e4%ba%8b%e7%89%a9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22285\/","title":{"rendered":"thinkphp\u5982\u4f55\u505a\u4e8b\u7269"},"content":{"rendered":"<p>thinkphp \u662f\u4e00\u6b3e\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5f00\u53d1 web \u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a\u8def\u7531\u3001\u6570\u636e\u5e93\u8bbf\u95ee\u3001\u6a21\u677f\u5f15\u64ce\u7b49\u7b49\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 thinkphp \u4e2d\u4f7f\u7528\u4e8b\u52a1\u3002<\/p>\n<p>\u4e00\u3001\u4ec0\u4e48\u662f\u4e8b\u52a1\uff1f<\/p>\n<p>\u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\uff0cA \u7ec4 SQL \u64cd\u4f5c\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u6574\u4f53\uff0c\u800c\u8fd9\u4e2a\u6574\u4f53\u7684\u6267\u884c\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002\u8fd9\u79cd\u884c\u4e3a\u88ab\u79f0\u4e3a\u4e8b\u52a1\u3002\u4e8b\u52a1\u4fdd\u62a4\u4e86\u4f60\u7684\u6570\u636e\u5e93\u514d\u53d7\u6570\u636e\u4e0d\u4e00\u81f4\u6027\u7684\u5a01\u80c1\uff0c\u540c\u65f6\u4e5f\u5141\u8bb8\u4f60\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u56de\u6eda\u4f60\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u4e8c\u3001\u5982\u4f55\u5728 ThinkPHP \u4e2d\u4f7f\u7528\u4e8b\u52a1\uff1f<\/p>\n<p>\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u6765\u4f7f\u7528\u4e8b\u52a1\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p>\u4f7f\u7528\u4e8b\u52a1\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\n<pre>$db=Db::connect();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u5f00\u59cb\u4e8b\u52a1<\/li>\n<\/ol>\n<p>\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528 beginTransaction() \u65b9\u6cd5\u6765\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1\u3002<\/p>\n<pre>$db-&gt;startTrans();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u6267\u884c SQL \u64cd\u4f5c<\/li>\n<\/ol>\n<p>\u5728\u4e8b\u52a1\u5f00\u59cb\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u6b63\u5e38\u7684\u65b9\u5f0f\u6765\u6267\u884c SQL \u64cd\u4f5c\u3002<\/p>\n<pre>$db-&gt;execute(\"INSERT INTO users (name, age) VALUES ('Tom', '18')\");\n$db-&gt;execute(\"UPDATE users SET age = '20' WHERE name = 'Tom'\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u63d0\u4ea4\u6216\u56de\u6eda\u4e8b\u52a1<\/li>\n<\/ol>\n<p>\u5728\u6240\u6709 SQL \u64cd\u4f5c\u90fd\u6267\u884c\u5b8c\u6bd5\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u60c5\u51b5\u6765\u63d0\u4ea4\u6216\u8005\u56de\u6eda\u4e8b\u52a1\u3002\u5982\u679c\u6240\u6709\u64cd\u4f5c\u90fd\u5b8c\u6210\u5e76\u4e14\u6ca1\u6709\u53d1\u73b0\u4efb\u4f55\u9519\u8bef\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 commit() \u65b9\u6cd5\u6765\u63d0\u4ea4\u4e8b\u52a1\u3002<\/p>\n<pre>$db-&gt;commit();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u9519\u8bef\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528 rollBack() \u65b9\u6cd5\u6765\u56de\u6eda\u4e8b\u52a1\u3002<\/p>\n<pre>$db-&gt;rollback();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u91ca\u653e\u6570\u636e\u5e93\u8fde\u63a5<\/li>\n<\/ol>\n<p>\u5728\u4e8b\u52a1\u7ed3\u675f\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u91ca\u653e\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<pre>$db = null;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u4e8b\u52a1\u7684\u9519\u8bef\u5904\u7406<\/p>\n<p>\u5728\u4e8b\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u5982\u679c\u51fa\u73b0\u4e86\u9519\u8bef\uff0c\u6211\u4eec\u9700\u8981\u5141\u8bb8\u7a0b\u5e8f\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u3002\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 try-catch \u8bed\u53e5\u6765\u5904\u7406\u9519\u8bef\u3002<\/p>\n<pre>try {\n$db=Db::connect();\n$db-&gt;startTrans();\n\/\/\u6267\u884c SQL \u64cd\u4f5c\n$db-&gt;commit();\n} catch (Exception $e) {\n$db-&gt;rollback();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5c06\u6574\u4e2a\u4e8b\u52a1\u7684\u64cd\u4f5c\u653e\u5728\u4e00\u4e2a try-catch \u8bed\u53e5\u5757\u91cc\u9762\u3002\u5728 try \u8bed\u53e5\u5757\u91cc\u9762\uff0c\u6211\u4eec\u6267\u884c SQL \u64cd\u4f5c\u5e76\u63d0\u4ea4\u4e8b\u52a1\u3002\u5982\u679c\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u9519\u8bef\uff0c\u6211\u4eec\u5c31\u4f1a\u8df3\u8f6c\u5230 catch \u8bed\u53e5\u5757\u91cc\u9762\uff0c\u5e76\u56de\u6eda\u6574\u4e2a\u4e8b\u52a1\u3002<\/p>\n<p>\u56db\u3001\u7ed3\u8bba<\/p>\n<p>\u4e8b\u52a1\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\u7684\u4e00\u79cd\u975e\u5e38\u91cd\u8981\u7684\u673a\u5236\uff0c\u5b83\u53ef\u4ee5\u4fdd\u62a4\u4f60\u7684\u6570\u636e\u5e93\u514d\u53d7\u6570\u636e\u4e0d\u4e00\u81f4\u6027\u7684\u5a01\u80c1\u3002\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e8b\u52a1\u6765\u6267\u884c\u4e00\u7ec4 SQL \u64cd\u4f5c\uff0c\u4ece\u800c\u786e\u4fdd\u5b83\u4eec\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e8b\u52a1\u7684\u4f7f\u7528\u9700\u8981\u8c28\u614e\uff0c\u5fc5\u987b\u4ed4\u7ec6\u5904\u7406\u9519\u8bef\uff0c\u4ee5\u514d\u51fa\u73b0\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u5982\u4f55\u505a\u4e8b\u7269\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>thinkphp \u662f\u4e00\u6b3e\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u5f0f\u6765\u5f00\u53d1 web \u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a\u8def\u7531\u3001\u6570\u636e\u5e93\u8bbf\u95ee\u3001\u6a21\u677f\u5f15\u64ce\u7b49\u7b49\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728 thinkphp \u4e2d\u4f7f\u7528\u4e8b\u52a1\u3002 \u4e00\u3001\u4ec0\u4e48\u662f\u4e8b\u52a1\uff1f \u5728\u5173\u7cfb\u578b\u6570\u636e\u5e93\u4e2d\uff0cA \u7ec4 SQL \u64cd\u4f5c\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u4e00\u4e2a\u6574\u4f53\uff0c\u800c\u8fd9\u4e2a\u6574\u4f53\u7684\u6267\u884c\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25\u3002\u8fd9\u79cd\u884c\u4e3a\u88ab\u79f0\u4e3a\u4e8b\u52a1\u3002\u4e8b\u52a1\u4fdd\u62a4\u4e86\u4f60\u7684\u6570\u636e\u5e93\u514d\u53d7\u6570\u636e\u4e0d\u4e00\u81f4\u6027\u7684\u5a01\u80c1\uff0c\u540c\u65f6\u4e5f\u5141\u8bb8\u4f60\u5728\u51fa\u73b0\u9519\u8bef\u65f6\u56de\u6eda\u4f60\u7684\u64cd\u4f5c\u3002 \u4e8c\u3001\u5982\u4f55\u5728 ThinkPHP \u4e2d\u4f7f\u7528\u4e8b\u52a1\uff1f \u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\u6765\u4f7f\u7528\u4e8b\u52a1\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5 \u4f7f\u7528\u4e8b\u52a1\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5728 ThinkPHP \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\uff1a $db=Db::connect(); \u767b\u5f55\u540e\u590d\u5236 \u5f00\u59cb\u4e8b\u52a1 \u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u8c03\u7528 beginTransaction() \u65b9\u6cd5\u6765\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1\u3002 $db-&gt;startTrans(); \u767b\u5f55\u540e\u590d\u5236 \u6267\u884c SQL \u64cd\u4f5c \u5728\u4e8b\u52a1\u5f00\u59cb\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u6b63\u5e38\u7684\u65b9\u5f0f\u6765\u6267\u884c SQL \u64cd\u4f5c\u3002 $db-&gt;execute(&#8220;INSERT INTO users (name, age) VALUES (&#8216;Tom&#8217;, &#8217;18&#8217;)&#8221;); $db-&gt;execute(&#8220;UPDATE users SET age = &#8217;20&#8217; WHERE name = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22285","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22285"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22285\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}