{"id":47551,"date":"2024-12-02T17:09:30","date_gmt":"2024-12-02T09:09:30","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47551\/"},"modified":"2024-12-02T17:09:30","modified_gmt":"2024-12-02T09:09:30","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e4%b8%8e%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e7%9a%84%e7%bb%93%e5%90%88","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47551\/","title":{"rendered":"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p>\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u53ef\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u5065\u58ee\u6027\u3002\u5f02\u5e38\u5904\u7406\u4f7f\u7528 try-catch \u8bed\u53e5\u5904\u7406\u9519\u8bef\uff0c\u63d0\u4f9b\u8be6\u7ec6\u9519\u8bef\u4fe1\u606f\u3002\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528 try-catch \u8bed\u53e5\u5305\u56f4\u6570\u636e\u5e93\u8fde\u63a5\u548c\u67e5\u8be2\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u5e76\u663e\u793a\u5728 catch \u5757\u4e2d\uff0c\u540c\u65f6\u786e\u4fdd\u6b63\u786e\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729923338671c890a1c053.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u800c\u65e0\u9700\u4e2d\u65ad\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u3002\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u8c03\u8bd5\u548c\u5904\u7406\u9519\u8bef\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u7528 try-catch \u8bed\u53e5\u6765\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p><strong>\u6570\u636e\u5e93\u64cd\u4f5c\u4e0e\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u53d1\u751f\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u8fde\u63a5\u5931\u8d25\u3001\u67e5\u8be2\u5931\u8d25\u548c\u8fdd\u53cd\u7ea6\u675f\u6761\u4ef6\u3002\u901a\u8fc7\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u53cd\u9988\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u6267\u884c\u67e5\u8be2\u7684\u5b9e\u6218\u6848\u4f8b\uff0c\u5176\u4e2d\u4f7f\u7528\u4e86\u5f02\u5e38\u5904\u7406\uff1a<\/p>\n<pre>&lt;?php\ntry {\n    \/\/ \u8fde\u63a5\u5230\u6570\u636e\u5e93\n    $conn = new PDO('mysql:host=localhost;dbname=mydb', 'username', 'password');\n\n    \/\/ \u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u6a21\u5f0f\n    $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n\n    \/\/ \u6267\u884c\u67e5\u8be2\n    $stmt = $conn-&gt;prepare('SELECT * FROM users WHERE id = :id');\n    $stmt-&gt;execute(['id' =&gt; 1]);\n\n    \/\/ \u63d0\u53d6\u7ed3\u679c\n    $result = $stmt-&gt;fetch(PDO::FETCH_ASSOC);\n} catch (PDOException $e) {\n    \/\/ \u5904\u7406\u5f02\u5e38\n    echo 'Error: ' . $e-&gt;getMessage();\n} finally {\n    \/\/ \u5173\u95ed\u8fde\u63a5\n    $conn-&gt;close();\n}\n?&gt;<\/pre>\n<p>\u5728\u6848\u4f8b\u4e2d\uff0c<code>try-catch<\/code> \u8bed\u53e5\u5305\u56f4\u4e86\u6570\u636e\u5e93\u8fde\u63a5\u548c\u67e5\u8be2\u64cd\u4f5c\u3002\u5982\u679c\u5728\u8fd9\u4e9b\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\uff0c\u5c06\u4f1a\u88ab\u6355\u83b7\u5e76\u663e\u793a\u5728 <code>catch<\/code> \u5757\u4e2d\u3002<code>finally<\/code> \u5757\u59cb\u7ec8\u5c06\u4f1a\u6267\u884c\uff0c\u786e\u4fdd\u6b63\u786e\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<p><strong>\u7ed3\u8bed<\/strong><\/p>\n<p>\u901a\u8fc7\u5c06\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u76f8\u7ed3\u5408\uff0c\u53ef\u4ee5\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u8c03\u8bd5\u6027\u3002\u901a\u8fc7\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bc6\u522b\u548c\u89e3\u51b3\u5728\u6570\u636e\u5e93\u4ea4\u4e92\u65f6\u53d1\u751f\u7684\u4efb\u4f55\u95ee\u9898\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408 \u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300aPHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002 \u7ed3\u5408\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u53ef\u589e\u5f3a\u5e94\u7528\u7a0b\u5e8f\u5065\u58ee\u6027\u3002\u5f02\u5e38\u5904\u7406\u4f7f\u7528 try-catch \u8bed\u53e5\u5904\u7406\u9519\u8bef\uff0c\u63d0\u4f9b\u8be6\u7ec6\u9519\u8bef\u4fe1\u606f\u3002\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0c\u901a\u8fc7\u4f7f\u7528 try-catch \u8bed\u53e5\u5305\u56f4\u6570\u636e\u5e93\u8fde\u63a5\u548c\u67e5\u8be2\u64cd\u4f5c\uff0c\u53ef\u4ee5\u6355\u83b7\u5f02\u5e38\u5e76\u663e\u793a\u5728 catch \u5757\u4e2d\uff0c\u540c\u65f6\u786e\u4fdd\u6b63\u786e\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002 PHP \u51fd\u6570\u5f02\u5e38\u5904\u7406\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7ed3\u5408 \u7b80\u4ecb \u5f02\u5e38\u5904\u7406\u5141\u8bb8\u5e94\u7528\u7a0b\u5e8f\u5904\u7406\u8fd0\u884c\u65f6\u9519\u8bef\uff0c\u800c\u65e0\u9700\u4e2d\u65ad\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u3002\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u4f9b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4ece\u800c\u66f4\u5bb9\u6613\u8c03\u8bd5\u548c\u5904\u7406\u9519\u8bef\u3002\u5728 PHP \u4e2d\uff0c\u53ef\u4ee5\u7528 try-catch \u8bed\u53e5\u6765\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002 \u6570\u636e\u5e93\u64cd\u4f5c\u4e0e\u5f02\u5e38\u5904\u7406 \u5728\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u53d1\u751f\u5404\u79cd\u5f02\u5e38\uff0c\u5982\u8fde\u63a5\u5931\u8d25\u3001\u67e5\u8be2\u5931\u8d25\u548c\u8fdd\u53cd\u7ea6\u675f\u6761\u4ef6\u3002\u901a\u8fc7\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u53ef\u4ee5\u4f18\u96c5\u5730\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u5e76\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u53cd\u9988\u3002 \u5b9e\u6218\u6848\u4f8b \u4ee5\u4e0b\u662f\u4e00\u4e2a\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u6267\u884c\u67e5\u8be2\u7684\u5b9e\u6218\u6848\u4f8b\uff0c\u5176\u4e2d\u4f7f\u7528\u4e86\u5f02\u5e38\u5904\u7406\uff1a &lt;?php try { \/\/ \u8fde\u63a5\u5230\u6570\u636e\u5e93 $conn = new PDO(&#8216;mysql:host=localhost;dbname=mydb&#8217;, &#8216;username&#8217;, &#8216;password&#8217;); \/\/ \u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u6a21\u5f0f $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); \/\/ \u6267\u884c\u67e5\u8be2 $stmt = $conn-&gt;prepare(&#8216;SELECT * FROM users WHERE id = :id&#8217;); $stmt-&gt;execute([&#8216;id&#8217; =&gt; 1]); \/\/ \u63d0\u53d6\u7ed3\u679c [&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-47551","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47551","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=47551"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47551\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}