{"id":164575,"date":"2024-11-05T09:03:29","date_gmt":"2024-11-05T01:03:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/164575\/"},"modified":"2024-11-05T09:03:30","modified_gmt":"2024-11-05T01:03:30","slug":"%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b5%8c%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e8%bc%b8%e5%87%ba-%e5%a6%82%e4%bd%95%e5%9c%a8%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e4%b8%8a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/164575\/","title":{"rendered":"\u975c\u614b\u7db2\u9801\u4e2d\u5982\u4f55\u5d4c\u5165\u6578\u64da\u5eab\u8f38\u51fa (\u5982\u4f55\u5728\u975c\u614b\u7db2\u9801\u4e0a\u8f38\u51fa\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e9%9d%9c%e6%85%8b%e7%b6%b2%e9%a0%81%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b5%8c%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e8%bc%b8%e5%87%ba-KGevqvOSQY\">\u975c\u614b\u7db2\u9801\u4e2d\u5982\u4f55\u5d4c\u5165\u6578\u64da\u5eab\u8f38\u51fa<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0c\u975c\u614b\u7db2\u9801\u901a\u5e38\u662f\u6307\u90a3\u4e9b\u4e0d\u6703\u96a8\u7528\u6236\u4e92\u52d5\u800c\u6539\u8b8a\u5167\u5bb9\u7684\u7db2\u9801\u3002\u9019\u985e\u7db2\u9801\u7684\u512a\u9ede\u5728\u65bc\u52a0\u8f09\u901f\u5ea6\u5feb\u3001\u6613\u65bc\u90e8\u7f72\uff0c\u4f46\u5176\u7f3a\u9ede\u662f\u7121\u6cd5\u76f4\u63a5\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u548c\u986f\u793a\u52d5\u614b\u5167\u5bb9\u3002\u7136\u800c\uff0c\u901a\u904e\u4e00\u4e9b\u6280\u8853\u624b\u6bb5\uff0c\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u5728\u975c\u614b\u7db2\u9801\u4e2d\u5d4c\u5165\u6578\u64da\u5eab\u8f38\u51fa\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5e7e\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\u4f86\u5be6\u73fe\u9019\u4e00\u76ee\u6a19\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e4%bc%ba%e6%9c%8d%e5%99%a8%e7%ab%af%e8%aa%9e%e8%a8%80%e7%94%9f%e6%88%90%e9%9d%9c%e6%85%8b%e5%85%a7%e5%ae%b9-KGevqvOSQY\">\u4f7f\u7528\u4f3a\u670d\u5668\u7aef\u8a9e\u8a00\u751f\u6210\u975c\u614b\u5167\u5bb9<\/h2>\n<p>\u4e00\u7a2e\u5e38\u898b\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4f3a\u670d\u5668\u7aef\u8a9e\u8a00\uff08\u5982 PHP\u3001Python \u6216 Node.js\uff09\u4f86\u751f\u6210\u975c\u614b\u7db2\u9801\u7684\u5167\u5bb9\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u57fa\u672c\u601d\u8def\u662f\uff0c\u5728\u4f3a\u670d\u5668\u4e0a\u904b\u884c\u4ee3\u78bc\uff0c\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\uff0c\u7136\u5f8c\u5c07\u9019\u4e9b\u6578\u64da\u5d4c\u5165\u5230\u975c\u614b HTML \u6587\u4ef6\u4e2d\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8-php-%e7%94%9f%e6%88%90%e9%9d%9c%e6%85%8b%e5%85%a7%e5%ae%b9-KGevqvOSQY\">\u793a\u4f8b\uff1a\u4f7f\u7528 PHP \u751f\u6210\u975c\u614b\u5167\u5bb9<\/h3>\n<pre><code>&lt;?php\n\/\/ \u9023\u63a5\u5230\u6578\u64da\u5eab\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database\";\n\n$conn = new mysqli($servername, $username, $password, $dbname);\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\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6578\u64da\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\uff0c\u6211\u5011\u9996\u5148\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u7136\u5f8c\u67e5\u8a62\u6587\u7ae0\u7684\u6a19\u984c\u548c\u5167\u5bb9\uff0c\u6700\u5f8c\u5c07\u9019\u4e9b\u6578\u64da\u8f38\u51fa\u5230 HTML \u4e2d\u3002\u9019\u6a23\u751f\u6210\u7684\u9801\u9762\u53ef\u4ee5\u88ab\u7528\u6236\u76f4\u63a5\u8a2a\u554f\uff0c\u4e26\u4e14\u5728\u6bcf\u6b21\u8acb\u6c42\u6642\u90fd\u6703\u986f\u793a\u6700\u65b0\u7684\u6578\u64da\u3002<\/p>\n<h2 id=\"%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-KGevqvOSQY\">\u4f7f\u7528\u975c\u614b\u7db2\u7ad9\u751f\u6210\u5668<\/h2>\n<p>\u975c\u614b\u7db2\u7ad9\u751f\u6210\u5668\uff08\u5982 Jekyll\u3001Hugo \u6216 Gatsby\uff09\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u69cb\u5efa\u975c\u614b\u7db2\u7ad9\u6642\u8f15\u9b06\u5730\u5d4c\u5165\u6578\u64da\u5eab\u5167\u5bb9\u3002\u9019\u4e9b\u5de5\u5177\u901a\u5e38\u652f\u6301\u5f9e\u6578\u64da\u6e90\uff08\u5982 JSON\u3001Markdown \u6216\u6578\u64da\u5eab\uff09\u63d0\u53d6\u6578\u64da\uff0c\u4e26\u5728\u69cb\u5efa\u904e\u7a0b\u4e2d\u751f\u6210\u975c\u614b\u9801\u9762\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8-jekyll-%e5%92%8c-json-%e6%95%b8%e6%93%9a-KGevqvOSQY\">\u793a\u4f8b\uff1a\u4f7f\u7528 Jekyll \u548c JSON \u6578\u64da<\/h3>\n<p>\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b JSON \u6587\u4ef6\uff0c\u5305\u542b\u6587\u7ae0\u6578\u64da\uff1a<\/p>\n<pre><code>[\n    {\n        \"title\": \"\u6587\u7ae0\u4e00\",\n        \"content\": \"\u9019\u662f\u6587\u7ae0\u4e00\u7684\u5167\u5bb9\u3002\"\n    },\n    {\n        \"title\": \"\u6587\u7ae0\u4e8c\",\n        \"content\": \"\u9019\u662f\u6587\u7ae0\u4e8c\u7684\u5167\u5bb9\u3002\"\n    }\n]\n<\/code><\/pre>\n<p>\u5728 Jekyll \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 Liquid \u6a21\u677f\u8a9e\u8a00\u4f86\u8b80\u53d6\u9019\u500b JSON \u6587\u4ef6\u4e26\u751f\u6210\u975c\u614b\u9801\u9762\uff1a<\/p>\n<pre><code>{% for article in site.data.articles %}\n  &lt;h2&gt;{{ article.title }}&lt;\/h2&gt;\n  &lt;p&gt;{{ article.content }}&lt;\/p&gt;\n{% endfor %}<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u7576\u6211\u5011\u69cb\u5efa\u7db2\u7ad9\u6642\uff0cJekyll \u6703\u81ea\u52d5\u5c07 JSON \u6578\u64da\u8f49\u63db\u70ba HTML\uff0c\u751f\u6210\u975c\u614b\u9801\u9762\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8-api-%e5%92%8c-javascript-KGevqvOSQY\">\u4f7f\u7528 API \u548c JavaScript<\/h2>\n<p>\u53e6\u4e00\u7a2e\u65b9\u6cd5\u662f\u4f7f\u7528 JavaScript \u548c API \u4f86\u52d5\u614b\u52a0\u8f09\u6578\u64da\u3002\u96d6\u7136\u9019\u7a2e\u65b9\u6cd5\u4e0d\u6703\u76f4\u63a5\u5c07\u6578\u64da\u5d4c\u5165\u975c\u614b\u9801\u9762\uff0c\u4f46\u53ef\u4ee5\u5728\u7528\u6236\u8a2a\u554f\u9801\u9762\u6642\u901a\u904e AJAX \u8acb\u6c42\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u6578\u64da\u3002<\/p>\n<h3 id=\"%e7%a4%ba%e4%be%8b%ef%bc%9a%e4%bd%bf%e7%94%a8-fetch-api-KGevqvOSQY\">\u793a\u4f8b\uff1a\u4f7f\u7528 Fetch API<\/h3>\n<pre><code>&lt;script&gt;\nfetch('https:\/\/example.com\/api\/articles')\n    .then(response =&gt; response.json())\n    .then(data =&gt; {\n        const container = document.getElementById('articles');\n        data.forEach(article =&gt; {\n            const articleElement = document.createElement('div');\n            articleElement.innerHTML = '&lt;h2&gt;' + article.title + '&lt;\/h2&gt;&lt;p&gt;' + article.content + '&lt;\/p&gt;';\n            container.appendChild(articleElement);\n        });\n    });\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u5728\u9019\u500b\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 Fetch API \u5f9e\u4e00\u500b\u5047\u8a2d\u7684 API \u7372\u53d6\u6587\u7ae0\u6578\u64da\uff0c\u7136\u5f8c\u5c07\u5176\u52d5\u614b\u6dfb\u52a0\u5230\u9801\u9762\u4e2d\u3002\u9019\u7a2e\u65b9\u6cd5\u7684\u512a\u9ede\u662f\u53ef\u4ee5\u96a8\u6642\u66f4\u65b0\u6578\u64da\uff0c\u800c\u4e0d\u9700\u8981\u91cd\u65b0\u751f\u6210\u975c\u614b\u9801\u9762\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-KGevqvOSQY\">\u7e3d\u7d50<\/h2>\n<p>\u96d6\u7136\u975c\u614b\u7db2\u9801\u672c\u8eab\u4e0d\u652f\u6301\u76f4\u63a5\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u6578\u64da\uff0c\u4f46\u901a\u904e\u4f7f\u7528\u4f3a\u670d\u5668\u7aef\u8a9e\u8a00\u3001\u975c\u614b\u7db2\u7ad9\u751f\u6210\u5668\u6216 JavaScript API\uff0c\u6211\u5011\u4ecd\u7136\u53ef\u4ee5\u5728\u975c\u614b\u7db2\u9801\u4e2d\u5d4c\u5165\u6578\u64da\u5eab\u8f38\u51fa\u3002\u9019\u4e9b\u65b9\u6cd5\u5404\u6709\u512a\u7f3a\u9ede\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u65b9\u6848\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u652f\u6301\u60a8\u7684\u7db2\u7ad9\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728\u975c\u614b\u7db2\u9801\u4e2d\u5d4c\u5165\u6578\u64da\u5eab\u8f38\u51fa\uff0c\u5be6\u73fe\u52d5\u614b\u5167\u5bb9\u5c55\u793a\uff0c\u63d0\u5347\u7db2\u7ad9\u4e92\u52d5\u6027\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-164575","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/164575","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=164575"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/164575\/revisions"}],"predecessor-version":[{"id":164576,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/164575\/revisions\/164576"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=164575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=164575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=164575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}