{"id":5413,"date":"2024-11-14T12:02:05","date_gmt":"2024-11-14T04:02:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5413\/"},"modified":"2024-11-14T12:02:05","modified_gmt":"2024-11-14T04:02:05","slug":"mysql%e5%bc%82%e5%b8%b8%e6%8d%95%e8%8e%b7%e6%80%8e%e4%b9%88%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5413\/","title":{"rendered":"mysql\u5f02\u5e38\u6355\u83b7\u600e\u4e48\u5904\u7406"},"content":{"rendered":"<blockquote><p>\n  mysql\u5f02\u5e38\u5904\u7406\u5305\u62ec\u4e24\u79cd\u65b9\u6cd5\uff1a1. try-catch \u5757\uff1b2. \u4f7f\u7528 mysqli_errno \u548c mysqli_error \u51fd\u6570\u3002\u5f02\u5e38\u5904\u7406\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\uff1a1. \u6355\u83b7\u5f02\u5e38\uff1b2. \u68c0\u67e5\u9519\u8bef\u4ee3\u7801\uff1b3. \u91c7\u53d6\u9002\u5f53\u884c\u52a8\uff0c\u5982\u7ec8\u6b62\u811a\u672c\u3001\u91cd\u8bd5\u67e5\u8be2\u6216\u901a\u77e5\u7528\u6237\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/28\/2024052811453293836.jpg\" class=\"aligncenter\" title=\"mysql\u5f02\u5e38\u6355\u83b7\u600e\u4e48\u5904\u7406\u63d2\u56fe\" alt=\"mysql\u5f02\u5e38\u6355\u83b7\u600e\u4e48\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u5f02\u5e38\u6355\u83b7\u5904\u7406<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 MySQL \u65f6\uff0c\u5f02\u5e38\u5904\u7406\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u5b83\u53ef\u4ee5\u9632\u6b62\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u5d29\u6e83\uff0c\u5e76\u5141\u8bb8\u7a0b\u5e8f\u4ee5\u4f18\u96c5\u7684\u65b9\u5f0f\u7ee7\u7eed\u8fd0\u884c\u6216\u63d0\u4f9b\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406 MySQL \u5f02\u5e38<\/strong><\/p>\n<p>\u5904\u7406 MySQL \u5f02\u5e38\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 try-catch \u5757<\/strong><\/p>\n<p>\u8fd9\u662f\u5904\u7406 PHP \u4e2d\u7684 MySQL \u5f02\u5e38\u7684\u9996\u9009\u65b9\u6cd5\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>try {\n    \/\/ \u6267\u884c MySQL \u67e5\u8be2\u6216\u64cd\u4f5c\n} catch (Exception $e) {\n    \/\/ \u5904\u7406\u5f02\u5e38\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u4f7f\u7528 i_errno \u548c mysqli_error<\/strong><\/p>\n<p>mysqli_errno() \u51fd\u6570\u8fd4\u56de\u9519\u8bef\u4ee3\u7801\uff0c\u800c mysqli_error() \u51fd\u6570\u8fd4\u56de\u9519\u8bef\u6d88\u606f\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u624b\u52a8\u5904\u7406\u5f02\u5e38\uff1a<\/p>\n<pre>if (mysqli_errno($conn)) {\n    \/\/ \u5904\u7406\u5f02\u5e38\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5f02\u5e38\u5904\u7406\u6b65\u9aa4<\/strong><\/p>\n<p>\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u5f02\u5e38\u5904\u7406\u901a\u5e38\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li> <strong>\u6355\u83b7\u5f02\u5e38\uff1a<\/strong>\u4f7f\u7528 try-catch \u5757\u6216 mysqli_errno() \u51fd\u6570\u6355\u83b7\u5f02\u5e38\u3002<\/li>\n<li> <strong>\u68c0\u67e5\u9519\u8bef\u4ee3\u7801\uff1a<\/strong>\u83b7\u53d6\u9519\u8bef\u4ee3\u7801\u4ee5\u786e\u5b9a\u5f02\u5e38\u7684\u7c7b\u578b\u3002<\/li>\n<li>\n<p><strong>\u91c7\u53d6\u9002\u5f53\u7684\u884c\u52a8\uff1a<\/strong>\u6839\u636e\u9519\u8bef\u4ee3\u7801\uff0c\u91c7\u53d6\u9002\u5f53\u7684\u884c\u52a8\uff0c\u4f8b\u5982\u4e0b\u5217\u52a8\u4f5c\uff1a<\/p>\n<ul>\n<li>\u7ec8\u6b62\u811a\u672c<\/li>\n<li>\u91cd\u8bd5\u67e5\u8be2<\/li>\n<li>\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f<\/li>\n<li>\u901a\u77e5\u7528\u6237<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4ee5\u4f7f\u7528 try-catch \u5757\u6355\u83b7\u5f02\u5e38\u4e3a\u4f8b\uff1a<\/p>\n<pre>try {\n    $stmt = $conn-&gt;prepare(\"SELECT * FROM users\");\n    $stmt-&gt;execute();\n    $result = $stmt-&gt;get_result();\n} catch (Exception $e) {\n    echo \"Error: \" . $e-&gt;getMessage();\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5f02\u5e38\u6355\u83b7\u600e\u4e48\u5904\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql\u5f02\u5e38\u5904\u7406\u5305\u62ec\u4e24\u79cd\u65b9\u6cd5\uff1a1. try-catch \u5757\uff1b2. \u4f7f\u7528 mysqli_errno \u548c mysqli_error \u51fd\u6570\u3002\u5f02\u5e38\u5904\u7406\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\uff1a1. \u6355\u83b7\u5f02\u5e38\uff1b2. \u68c0\u67e5\u9519\u8bef\u4ee3\u7801\uff1b3. \u91c7\u53d6\u9002\u5f53\u884c\u52a8\uff0c\u5982\u7ec8\u6b62\u811a\u672c\u3001\u91cd\u8bd5\u67e5\u8be2\u6216\u901a\u77e5\u7528\u6237\u3002 MySQL \u5f02\u5e38\u6355\u83b7\u5904\u7406 \u5728\u4f7f\u7528 MySQL \u65f6\uff0c\u5f02\u5e38\u5904\u7406\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u5b83\u53ef\u4ee5\u9632\u6b62\u7a0b\u5e8f\u5728\u9047\u5230\u9519\u8bef\u65f6\u5d29\u6e83\uff0c\u5e76\u5141\u8bb8\u7a0b\u5e8f\u4ee5\u4f18\u96c5\u7684\u65b9\u5f0f\u7ee7\u7eed\u8fd0\u884c\u6216\u63d0\u4f9b\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002 \u5982\u4f55\u5904\u7406 MySQL \u5f02\u5e38 \u5904\u7406 MySQL \u5f02\u5e38\u6709\u4e24\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a 1. \u4f7f\u7528 try-catch \u5757 \u8fd9\u662f\u5904\u7406 PHP \u4e2d\u7684 MySQL \u5f02\u5e38\u7684\u9996\u9009\u65b9\u6cd5\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a try { \/\/ \u6267\u884c MySQL \u67e5\u8be2\u6216\u64cd\u4f5c } catch (Exception $e) { \/\/ \u5904\u7406\u5f02\u5e38 } \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 i_errno \u548c mysqli_error mysqli_errno() \u51fd\u6570\u8fd4\u56de\u9519\u8bef\u4ee3\u7801\uff0c\u800c mysqli_error() \u51fd\u6570\u8fd4\u56de\u9519\u8bef\u6d88\u606f\u3002\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u51fd\u6570\u6765\u624b\u52a8\u5904\u7406\u5f02\u5e38\uff1a [&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-5413","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5413","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=5413"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5413\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}