{"id":47786,"date":"2024-12-02T08:25:19","date_gmt":"2024-12-02T00:25:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47786\/"},"modified":"2024-12-02T08:25:19","modified_gmt":"2024-12-02T00:25:19","slug":"php-%e5%87%bd%e6%95%b0%e5%8f%82%e6%95%b0%e7%bb%91%e5%ae%9a%e4%b8%ad%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e5%92%8c%e5%bc%82%e5%b8%b8%e6%9c%ba%e5%88%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47786\/","title":{"rendered":"PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f<\/h1>\n<p>\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f\u300b<\/span>\u5c06\u4f1a\u4ecb\u7ecd\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p>PHP\u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u7684\u9519\u8bef\u5904\u7406\u6d89\u53ca\u9519\u8bef\u6d88\u606f\u83b7\u53d6\uff08\u4f7f\u7528mysqli_stmt_error()\uff09\uff0c\u800c\u5f02\u5e38\u673a\u5236\u901a\u8fc7mysqli_stmt_set_exception()\u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u5668\uff0c\u5e76\u5728\u9519\u8bef\u53d1\u751f\u65f6\u629b\u51famysqli_sql_exception\u5f02\u5e38\uff0c\u53ef\u901a\u8fc7try-catch\u5757\u6355\u83b7\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729936068671cbac42063f.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u7684\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236<\/strong><\/p>\n<p>\u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\u6280\u672f\u53ef\u4ee5\u6709\u6548\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\uff0c\u4f46\u540c\u65f6\uff0c\u5b83\u4e5f\u5f15\u5165\u4e86\u5bf9\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\u7684\u5168\u65b0\u6311\u6218\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 PHP \u4e2d\u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u7684\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u5b83\u3002<\/p>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p>\u5f53 PHP \u51fd\u6570\u4e2d\u7684\u53c2\u6570\u7ed1\u5b9a\u8fc7\u7a0b\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u5c06\u751f\u6210\u4e00\u4e2a <code>mysqli_stmt_error()<\/code> \u51fd\u6570\u8fd4\u56de\u7684\u9519\u8bef\u6d88\u606f\u3002\u8981\u83b7\u53d6\u9519\u8bef\u6d88\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>$error = mysqli_stmt_error($stmt);<\/pre>\n<pre>\u4f7f\u7528 mysqli_stmt_error($stmt) \u83b7\u53d6\u9519\u8bef\u6d88\u606f<\/pre>\n<p><strong>\u5f02\u5e38\u673a\u5236<\/strong><\/p>\n<p>\u9664\u4e86\u9519\u8bef\u5904\u7406\u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 PHP \u4e2d\u7684\u5f02\u5e38\u673a\u5236\u6765\u5904\u7406\u53c2\u6570\u7ed1\u5b9a\u4e2d\u7684\u9519\u8bef\u3002\u4e3a\u4e86\u542f\u7528\u6b64\u529f\u80fd\uff0c\u9700\u8981\u4f7f\u7528 <code>mysqli_stmt::set_exception()<\/code> \u51fd\u6570\u6765\u8bbe\u7f6e\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u5668\uff1a<\/p>\n<pre>mysqli_stmt_set_exception($stmt, mysqli::ATTR_ERRNO, $errno);<\/pre>\n<pre>\u4f7f\u7528 mysqli_stmt_set_exception($stmt, mysqli::ATTR_ERRNO, $errno) \u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u5668<\/pre>\n<p>\u5f53\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u5c06\u629b\u51fa\u4e00\u4e2a <code>mysqli_sql_exception<\/code> \u5f02\u5e38\uff0c\u5176\u4e2d\u5305\u542b\u9519\u8bef\u4ee3\u7801\u548c\u6d88\u606f\u3002\u8981\u6355\u83b7\u6b64\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a <code>try-catch<\/code> \u5757\uff1a<\/p>\n<pre>try {\n    \/\/ \u6267\u884c\u7ed1\u5b9a\u64cd\u4f5c\n} catch (mysqli_sql_exception $e) {\n    \/\/ \u5904\u7406\u9519\u8bef\n}<\/pre>\n<pre>\u4f7f\u7528 try-catch \u5757\u6355\u83b7 mysqli_sql_exception<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\u5e76\u542f\u7528\u5f02\u5e38\u673a\u5236\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre>&lt;?php\n\n\/\/ \u521b\u5efa\u4e00\u4e2a MySQLi \u8bed\u53e5\n$stmt = $mysqli-&gt;prepare(\"INSERT INTO users (username, email, password) VALUES (?, ?, ?)\");\n\n\/\/ \u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u5668\nmysqli_stmt_set_exception($stmt, mysqli::ATTR_ERRNO, $errno);\n\ntry {\n    \/\/ \u7ed1\u5b9a\u53c2\u6570\n    $stmt-&gt;bind_param(\"sss\", $username, $email, $password);\n\n    \/\/ \u6267\u884c\u8bed\u53e5\n    $stmt-&gt;execute();\n} catch (mysqli_sql_exception $e) {\n    \/\/ \u5904\u7406\u9519\u8bef\n    echo \"Error occurred: \" . $e-&gt;getMessage();\n}\n\n?&gt;<\/pre>\n<pre>\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\u5e76\u542f\u7528\u5f02\u5e38\u673a\u5236\u7684\u5b9e\u6218\u6848\u4f8b<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u638c\u63e1 PHP \u4e2d\u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u7684\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\u5bf9\u4e8e\u7f16\u5199\u5065\u58ee\u4e14\u5b89\u5168\u7684\u7a0b\u5e8f\u81f3\u5173\u91cd\u8981\u3002\u901a\u8fc7\u7406\u89e3\u8fd9\u4e9b\u673a\u5236\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u5904\u7406\u53c2\u6570\u7ed1\u5b9a\u8fc7\u7a0b\u4e2d\u7684\u9519\u8bef\u5e76\u63d0\u4f9b\u6709\u610f\u4e49\u7684\u9519\u8bef\u6d88\u606f\u7ed9\u7528\u6237\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u53c2\u6570\u7ed1\u5b9a\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f \u5b66\u4e60\u6587\u7ae0\u8981\u52aa\u529b\uff0c\u4f46\u662f\u4e0d\u8981\u6025\uff01\u4eca\u5929\u7684\u8fd9\u7bc7\u6587\u7ae0\u300aPHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff1f\u300b\u5c06\u4f1a\u4ecb\u7ecd\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u60f3\u6df1\u5165\u5b66\u4e60\u6587\u7ae0\uff0c\u53ef\u4ee5\u5173\u6ce8\u6211\uff01\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\u7684\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u80fd\u6709\u6240\u5e2e\u52a9\uff01 PHP\u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u7684\u9519\u8bef\u5904\u7406\u6d89\u53ca\u9519\u8bef\u6d88\u606f\u83b7\u53d6\uff08\u4f7f\u7528mysqli_stmt_error()\uff09\uff0c\u800c\u5f02\u5e38\u673a\u5236\u901a\u8fc7mysqli_stmt_set_exception()\u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u5668\uff0c\u5e76\u5728\u9519\u8bef\u53d1\u751f\u65f6\u629b\u51famysqli_sql_exception\u5f02\u5e38\uff0c\u53ef\u901a\u8fc7try-catch\u5757\u6355\u83b7\u3002 PHP \u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u4e2d\u7684\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236 \u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53c2\u6570\u7ed1\u5b9a\u6280\u672f\u53ef\u4ee5\u6709\u6548\u9632\u6b62 SQL \u6ce8\u5165\u653b\u51fb\uff0c\u4f46\u540c\u65f6\uff0c\u5b83\u4e5f\u5f15\u5165\u4e86\u5bf9\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\u7684\u5168\u65b0\u6311\u6218\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8 PHP \u4e2d\u51fd\u6570\u53c2\u6570\u7ed1\u5b9a\u7684\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u673a\u5236\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u6765\u8bf4\u660e\u5982\u4f55\u4f7f\u7528\u5b83\u3002 \u9519\u8bef\u5904\u7406 \u5f53 PHP \u51fd\u6570\u4e2d\u7684\u53c2\u6570\u7ed1\u5b9a\u8fc7\u7a0b\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u5c06\u751f\u6210\u4e00\u4e2a mysqli_stmt_error() \u51fd\u6570\u8fd4\u56de\u7684\u9519\u8bef\u6d88\u606f\u3002\u8981\u83b7\u53d6\u9519\u8bef\u6d88\u606f\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a $error = mysqli_stmt_error($stmt); \u4f7f\u7528 mysqli_stmt_error($stmt) \u83b7\u53d6\u9519\u8bef\u6d88\u606f \u5f02\u5e38\u673a\u5236 \u9664\u4e86\u9519\u8bef\u5904\u7406\u4e4b\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 PHP \u4e2d\u7684\u5f02\u5e38\u673a\u5236\u6765\u5904\u7406\u53c2\u6570\u7ed1\u5b9a\u4e2d\u7684\u9519\u8bef\u3002\u4e3a\u4e86\u542f\u7528\u6b64\u529f\u80fd\uff0c\u9700\u8981\u4f7f\u7528 mysqli_stmt::set_exception() \u51fd\u6570\u6765\u8bbe\u7f6e\u4e00\u4e2a\u5f02\u5e38\u5904\u7406\u5668\uff1a mysqli_stmt_set_exception($stmt, mysqli::ATTR_ERRNO, $errno); \u4f7f\u7528 mysqli_stmt_set_exception($stmt, mysqli::ATTR_ERRNO, $errno) \u8bbe\u7f6e\u5f02\u5e38\u5904\u7406\u5668 \u5f53\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u5c06\u629b\u51fa\u4e00\u4e2a mysqli_sql_exception \u5f02\u5e38\uff0c\u5176\u4e2d\u5305\u542b\u9519\u8bef\u4ee3\u7801\u548c\u6d88\u606f\u3002\u8981\u6355\u83b7\u6b64\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a try-catch \u5757\uff1a try { \/\/ \u6267\u884c\u7ed1\u5b9a\u64cd\u4f5c } catch (mysqli_sql_exception $e) { \/\/ [&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-47786","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47786","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=47786"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47786\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}