{"id":173645,"date":"2024-11-07T19:45:20","date_gmt":"2024-11-07T11:45:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/173645\/"},"modified":"2024-11-07T19:45:21","modified_gmt":"2024-11-07T11:45:21","slug":"%e5%a6%82%e4%bd%95%e5%b0%87%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b5%90%e5%90%88%ef%bc%9f-%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e6%80%8e%e9%ba%bc%e5%8a%a0%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/173645\/","title":{"rendered":"\u5982\u4f55\u5c07\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7d50\u5408\uff1f (\u975c\u614b\u7db2\u9801\u600e\u9ebc\u52a0\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e5%b0%87%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b5%90%e5%90%88%ef%bc%9f-vOQKKJefJs\">\u5982\u4f55\u5c07\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7d50\u5408\uff1f<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0c\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7684\u7d50\u5408\u5df2\u6210\u70ba\u4e00\u500b\u91cd\u8981\u7684\u8ab2\u984c\u3002\u975c\u614b\u7db2\u9801\u901a\u5e38\u662f\u6307\u90a3\u4e9b\u4e0d\u6703\u96a8\u8457\u7528\u6236\u7684\u4e92\u52d5\u800c\u6539\u8b8a\u5167\u5bb9\u7684\u7db2\u9801\uff0c\u800c\u6578\u64da\u5eab\u5247\u662f\u7528\u4f86\u5b58\u5132\u548c\u7ba1\u7406\u6578\u64da\u7684\u7cfb\u7d71\u3002\u5c07\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7d50\u5408\uff0c\u53ef\u4ee5\u4f7f\u7db2\u7ad9\u5177\u5099\u52d5\u614b\u5167\u5bb9\u7684\u80fd\u529b\uff0c\u5f9e\u800c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u548c\u7db2\u7ad9\u7684\u529f\u80fd\u6027\u3002<\/p>\n<h2 id=\"%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e7%9a%84%e7%89%b9%e9%bb%9e-vOQKKJefJs\">\u975c\u614b\u7db2\u9801\u7684\u7279\u9ede<\/h2>\n<p>\u975c\u614b\u7db2\u9801\u7684\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u5167\u5bb9\u56fa\u5b9a\uff1a\u975c\u614b\u7db2\u9801\u7684\u5167\u5bb9\u5728\u4f3a\u670d\u5668\u4e0a\u662f\u56fa\u5b9a\u7684\uff0c\u7121\u6cd5\u6839\u64da\u7528\u6236\u7684\u8acb\u6c42\u9032\u884c\u8b8a\u66f4\u3002<\/li>\n<li>\u52a0\u8f09\u901f\u5ea6\u5feb\uff1a\u7531\u65bc\u975c\u614b\u7db2\u9801\u4e0d\u9700\u8981\u9032\u884c\u6578\u64da\u5eab\u67e5\u8a62\uff0c\u56e0\u6b64\u52a0\u8f09\u901f\u5ea6\u901a\u5e38\u8f03\u5feb\u3002<\/li>\n<li>\u7c21\u55ae\u6613\u65bc\u90e8\u7f72\uff1a\u975c\u614b\u7db2\u9801\u53ef\u4ee5\u76f4\u63a5\u653e\u7f6e\u5728\u4f3a\u670d\u5668\u4e0a\uff0c\u7121\u9700\u984d\u5916\u7684\u5f8c\u7aef\u6280\u8853\u652f\u6301\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e4%bd%9c%e7%94%a8-vOQKKJefJs\">\u6578\u64da\u5eab\u7684\u4f5c\u7528<\/h2>\n<p>\u6578\u64da\u5eab\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5b58\u5132\u548c\u7ba1\u7406\u6578\u64da\u3002\u5e38\u898b\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u5305\u62ec MySQL\u3001PostgreSQL \u548c MongoDB \u7b49\u3002\u901a\u904e\u6578\u64da\u5eab\uff0c\u958b\u767c\u8005\u53ef\u4ee5\uff1a<\/p>\n<ul>\n<li>\u52d5\u614b\u751f\u6210\u5167\u5bb9\uff1a\u6839\u64da\u7528\u6236\u7684\u8acb\u6c42\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\uff0c\u4e26\u52d5\u614b\u751f\u6210\u7db2\u9801\u5167\u5bb9\u3002<\/li>\n<li>\u6578\u64da\u7ba1\u7406\uff1a\u65b9\u4fbf\u5730\u5c0d\u6578\u64da\u9032\u884c\u589e\u522a\u6539\u67e5\u64cd\u4f5c\u3002<\/li>\n<li>\u63d0\u9ad8\u7db2\u7ad9\u7684\u53ef\u64f4\u5c55\u6027\uff1a\u96a8\u8457\u6578\u64da\u91cf\u7684\u589e\u52a0\uff0c\u6578\u64da\u5eab\u53ef\u4ee5\u8f15\u9b06\u64f4\u5c55\u4ee5\u6eff\u8db3\u9700\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e8%88%87%e6%95%b8%e6%93%9a%e5%ba%ab%e7%b5%90%e5%90%88%e7%9a%84%e6%96%b9%e5%bc%8f-vOQKKJefJs\">\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7d50\u5408\u7684\u65b9\u5f0f<\/h2>\n<p>\u5c07\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7d50\u5408\u7684\u65b9\u5f0f\u6709\u591a\u7a2e\uff0c\u4ee5\u4e0b\u662f\u5e7e\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\uff1a<\/p>\n<h3 id=\"1-%e4%bd%bf%e7%94%a8%e5%be%8c%e7%ab%af%e8%aa%9e%e8%a8%80%e7%94%9f%e6%88%90%e5%8b%95%e6%85%8b%e5%85%a7%e5%ae%b9-vOQKKJefJs\">1. \u4f7f\u7528\u5f8c\u7aef\u8a9e\u8a00\u751f\u6210\u52d5\u614b\u5167\u5bb9<\/h3>\n<p>\u6700\u5e38\u898b\u7684\u65b9\u5f0f\u662f\u4f7f\u7528\u5f8c\u7aef\u8a9e\u8a00\uff08\u5982 PHP\u3001Python \u6216 Node.js\uff09\u4f86\u8655\u7406\u6578\u64da\u5eab\u8acb\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528 PHP \u548c MySQL \u7684\u7c21\u55ae\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\n$conn = new mysqli(\"localhost\", \"username\", \"password\", \"database\");\n\n\/\/ \u6aa2\u67e5\u9023\u63a5\nif ($conn-&gt;connect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u67e5\u8a62\u6578\u64da\n$sql = \"SELECT title, content FROM articles\";\n$result = $conn-&gt;query($sql);\n\n\/\/ \u986f\u793a\u6578\u64da\nif ($result-&gt;num_rows &gt; 0) {\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"&lt;h2&gt;\" . $row[\"title\"] . \"&lt;\/h2&gt;\";\n        echo \"&lt;p&gt;\" . $row[\"content\"] . \"&lt;\/p&gt;\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0cPHP \u4ee3\u78bc\u9023\u63a5\u5230 MySQL \u6578\u64da\u5eab\uff0c\u67e5\u8a62\u6587\u7ae0\u6a19\u984c\u548c\u5167\u5bb9\uff0c\u4e26\u5c07\u5176\u986f\u793a\u5728\u975c\u614b\u7db2\u9801\u4e0a\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8-api-%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a-vOQKKJefJs\">2. \u4f7f\u7528 API \u7372\u53d6\u6578\u64da<\/h3>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u901a\u904e API \u7372\u53d6\u6578\u64da\u3002\u9019\u7a2e\u65b9\u5f0f\u9069\u5408\u65bc\u9700\u8981\u5f9e\u4e0d\u540c\u4f86\u6e90\u7372\u53d6\u6578\u64da\u7684\u60c5\u6cc1\u3002\u53ef\u4ee5\u4f7f\u7528 JavaScript \u7684 Fetch API \u4f86\u7372\u53d6\u6578\u64da\uff1a<\/p>\n<pre><code>fetch(\"https:\/\/api.example.com\/articles\")\n    .then(response =&gt; response.json())\n    .then(data =&gt; {\n        data.forEach(article =&gt; {\n            document.body.innerHTML += \"&lt;h2&gt;\" + article.title + \"&lt;\/h2&gt;\";\n            document.body.innerHTML += \"&lt;p&gt;\" + article.content + \"&lt;\/p&gt;\";\n        });\n    })\n    .catch(error =&gt; console.error(\"\u932f\u8aa4:\", error));<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u901a\u904e Fetch API \u5f9e\u6307\u5b9a\u7684 API \u7372\u53d6\u6587\u7ae0\u6578\u64da\uff0c\u7136\u5f8c\u5c07\u5176\u52d5\u614b\u6dfb\u52a0\u5230\u975c\u614b\u7db2\u9801\u4e2d\u3002<\/p>\n<h3 id=\"3-%e4%bd%bf%e7%94%a8%e9%9d%9c%e6%85%8b%e7%b6%b2%e7%ab%99%e7%94%9f%e6%88%90%e5%99%a8-vOQKKJefJs\">3. \u4f7f\u7528\u975c\u614b\u7db2\u7ad9\u751f\u6210\u5668<\/h3>\n<p>\u975c\u614b\u7db2\u7ad9\u751f\u6210\u5668\uff08\u5982 Gatsby \u6216 Jekyll\uff09\u53ef\u4ee5\u5728\u69cb\u5efa\u6642\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\uff0c\u751f\u6210\u975c\u614b\u9801\u9762\u3002\u9019\u7a2e\u65b9\u6cd5\u7d50\u5408\u4e86\u975c\u614b\u7db2\u9801\u7684\u512a\u52e2\u548c\u6578\u64da\u5eab\u7684\u9748\u6d3b\u6027\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-vOQKKJefJs\">\u7e3d\u7d50<\/h2>\n<p>\u5c07\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7d50\u5408\uff0c\u53ef\u4ee5\u4f7f\u7db2\u7ad9\u5177\u5099\u66f4\u5f37\u7684\u529f\u80fd\u6027\u548c\u4e92\u52d5\u6027\u3002\u7121\u8ad6\u662f\u901a\u904e\u5f8c\u7aef\u8a9e\u8a00\u751f\u6210\u52d5\u614b\u5167\u5bb9\u3001\u4f7f\u7528 API \u7372\u53d6\u6578\u64da\uff0c\u9084\u662f\u5229\u7528\u975c\u614b\u7db2\u7ad9\u751f\u6210\u5668\uff0c\u958b\u767c\u8005\u90fd\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e00\u76ee\u6a19\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u80fd\u548c\u7a69\u5b9a\u6027\u7684\u7db2\u7ad9\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u6216 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u4f86\u90e8\u7f72\u7db2\u7ad9\u4e5f\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5c07\u975c\u614b\u7db2\u9801\u8207\u6578\u64da\u5eab\u7d50\u5408\uff0c\u5be6\u73fe\u52d5\u614b\u5167\u5bb9\u66f4\u65b0\uff0c\u63d0\u5347\u7db2\u7ad9\u529f\u80fd\u8207\u7528\u6236\u9ad4\u9a57\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-173645","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/173645","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=173645"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/173645\/revisions"}],"predecessor-version":[{"id":173646,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/173645\/revisions\/173646"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=173645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=173645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=173645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}