{"id":183372,"date":"2024-11-10T04:16:23","date_gmt":"2024-11-09T20:16:23","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183372\/"},"modified":"2024-11-10T04:16:24","modified_gmt":"2024-11-09T20:16:24","slug":"%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%abcgi%e7%9a%84%e7%a5%9e%e5%99%a8%ef%bc%8c%e7%b0%a1%e6%bd%94%e9%ab%98%e6%95%88%e7%9a%84%e5%87%bd%e6%95%b8-%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%abcgi","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183372\/","title":{"rendered":"\u5be6\u73fe\u6578\u64da\u5eabCGI\u7684\u795e\u5668\uff0c\u7c21\u6f54\u9ad8\u6548\u7684\u51fd\u6578 (\u5be6\u73fe\u6578\u64da\u5eabcgi\u7684\u51fd\u6578)"},"content":{"rendered":"<h1 id=\"%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%abcgi%e7%9a%84%e7%a5%9e%e5%99%a8%ef%bc%8c%e7%b0%a1%e6%bd%94%e9%ab%98%e6%95%88%e7%9a%84%e5%87%bd%e6%95%b8-EGglzPRJtH\">\u5be6\u73fe\u6578\u64da\u5eabCGI\u7684\u795e\u5668\uff0c\u7c21\u6f54\u9ad8\u6548\u7684\u51fd\u6578<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u61c9\u7528\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u4f7f\u7528\u5df2\u7d93\u6210\u70ba\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u7db2\u7ad9\u9084\u662f\u5927\u578b\u4f01\u696d\u7cfb\u7d71\uff0c\u6578\u64da\u5eab\u90fd\u626e\u6f14\u8457\u95dc\u9375\u7684\u89d2\u8272\u3002CGI\uff08Common Gateway Interface\uff09\u4f5c\u70ba\u4e00\u7a2e\u6a19\u6e96\u5354\u8b70\uff0c\u5141\u8a31\u7db2\u9801\u8207\u5916\u90e8\u7a0b\u5e8f\u9032\u884c\u4ea4\u4e92\uff0c\u5f9e\u800c\u5be6\u73fe\u52d5\u614b\u5167\u5bb9\u7684\u751f\u6210\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u901a\u904e\u7c21\u6f54\u9ad8\u6548\u7684\u51fd\u6578\u4f86\u5be6\u73fe\u6578\u64da\u5eabCGI\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u4ee3\u78bc\u793a\u4f8b\u3002<\/p>\n<h2 id=\"cgi%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-EGglzPRJtH\">CGI\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>CGI\u662f\u4e00\u7a2e\u5354\u8b70\uff0c\u5141\u8a31Web\u4f3a\u670d\u5668\u8207\u5916\u90e8\u7a0b\u5e8f\uff08\u5982\u8173\u672c\u6216\u61c9\u7528\u7a0b\u5e8f\uff09\u9032\u884c\u901a\u4fe1\u3002\u7576\u7528\u6236\u5728\u700f\u89bd\u5668\u4e2d\u8acb\u6c42\u67d0\u500b\u7db2\u9801\u6642\uff0c\u4f3a\u670d\u5668\u53ef\u4ee5\u901a\u904eCGI\u8abf\u7528\u76f8\u61c9\u7684\u7a0b\u5e8f\u4f86\u751f\u6210\u52d5\u614b\u5167\u5bb9\u3002\u9019\u4e9b\u7a0b\u5e8f\u53ef\u4ee5\u662f\u7528\u591a\u7a2e\u7de8\u7a0b\u8a9e\u8a00\u7de8\u5beb\u7684\uff0c\u5982Perl\u3001Python\u3001PHP\u7b49\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e5%bf%85%e8%a6%81%e6%80%a7-EGglzPRJtH\">\u6578\u64da\u5eab\u9023\u63a5\u7684\u5fc5\u8981\u6027<\/h2>\n<p>\u5728CGI\u7a0b\u5e8f\u4e2d\uff0c\u6578\u64da\u5eab\u9023\u63a5\u662f\u5be6\u73fe\u6578\u64da\u5b58\u53d6\u7684\u95dc\u9375\u3002\u7121\u8ad6\u662f\u67e5\u8a62\u6578\u64da\u9084\u662f\u66f4\u65b0\u6578\u64da\uff0c\u826f\u597d\u7684\u6578\u64da\u5eab\u9023\u63a5\u51fd\u6578\u90fd\u80fd\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\u548c\u7a69\u5b9a\u6027\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u8a9e\u8a00\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u7684\u57fa\u672c\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\nfunction connectDatabase($host, $user, $password, $dbname) {\n    $conn = new mysqli($host, $user, $password, $dbname);\n    if ($conn-&gt;connect_error) {\n        die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n    }\n    return $conn;\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b0%a1%e6%bd%94%e9%ab%98%e6%95%88%e7%9a%84%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c%e5%87%bd%e6%95%b8-EGglzPRJtH\">\u7c21\u6f54\u9ad8\u6548\u7684\u6578\u64da\u5eab\u64cd\u4f5c\u51fd\u6578<\/h2>\n<p>\u70ba\u4e86\u63d0\u9ad8\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u6548\u7387\uff0c\u6211\u5011\u53ef\u4ee5\u7de8\u5beb\u4e00\u4e9b\u7c21\u6f54\u7684\u51fd\u6578\u4f86\u57f7\u884c\u5e38\u898b\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5982\u67e5\u8a62\u3001\u63d2\u5165\u3001\u66f4\u65b0\u548c\u522a\u9664\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\u51fd\u6578\uff1a<\/p>\n<h3 id=\"%e6%9f%a5%e8%a9%a2%e5%87%bd%e6%95%b8-EGglzPRJtH\">\u67e5\u8a62\u51fd\u6578<\/h3>\n<pre><code>&lt;?php\nfunction fetchData($conn, $query) {\n    $result = $conn-&gt;query($query);\n    if ($result-&gt;num_rows &gt; 0) {\n        return $result-&gt;fetch_all(MYSQLI_ASSOC);\n    } else {\n        return [];\n    }\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"%e6%8f%92%e5%85%a5%e5%87%bd%e6%95%b8-EGglzPRJtH\">\u63d2\u5165\u51fd\u6578<\/h3>\n<pre><code>&lt;?php\nfunction insertData($conn, $table, $data) {\n    $columns = implode(\", \", array_keys($data));\n    $values = implode(\", \", array_map(function($value) use ($conn) {\n        return \"'\" . $conn-&gt;real_escape_string($value) . \"'\";\n    }, array_values($data)));\n    \n    $query = \"INSERT INTO $table ($columns) VALUES ($values)\";\n    return $conn-&gt;query($query);\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"%e6%9b%b4%e6%96%b0%e5%87%bd%e6%95%b8-EGglzPRJtH\">\u66f4\u65b0\u51fd\u6578<\/h3>\n<pre><code>&lt;?php\nfunction updateData($conn, $table, $data, $condition) {\n    $set = implode(\", \", array_map(function($key, $value) use ($conn) {\n        return \"$key = '\" . $conn-&gt;real_escape_string($value) . \"'\";\n    }, array_keys($data), $data));\n    \n    $query = \"UPDATE $table SET $set WHERE $condition\";\n    return $conn-&gt;query($query);\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"%e5%88%aa%e9%99%a4%e5%87%bd%e6%95%b8-EGglzPRJtH\">\u522a\u9664\u51fd\u6578<\/h3>\n<pre><code>&lt;?php\nfunction deleteData($conn, $table, $condition) {\n    $query = \"DELETE FROM $table WHERE $condition\";\n    return $conn-&gt;query($query);\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-EGglzPRJtH\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4e0a\u8ff0\u7c21\u6f54\u9ad8\u6548\u7684\u51fd\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5be6\u73fe\u6578\u64da\u5eabCGI\u7684\u529f\u80fd\uff0c\u5f9e\u800c\u63d0\u9ad8\u7db2\u9801\u61c9\u7528\u7684\u52d5\u614b\u6027\u548c\u4ea4\u4e92\u6027\u3002\u9019\u4e9b\u51fd\u6578\u4e0d\u50c5\u7c21\u5316\u4e86\u6578\u64da\u5eab\u64cd\u4f5c\u7684\u904e\u7a0b\uff0c\u9084\u63d0\u9ad8\u4e86\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u7dad\u8b77\u6027\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9019\u4e9b\u5de5\u5177\u7121\u7591\u662f\u958b\u767c\u8005\u7684\u5f97\u529b\u52a9\u624b\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u7db2\u7ad9\u9084\u662f\u5927\u578b\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7c21\u6f54\u9ad8\u6548\u7684\u51fd\u6578\uff0c\u5be6\u73fe\u6578\u64da\u5eabCGI\u7684\u795e\u5668\uff0c\u52a9\u60a8\u8f15\u9b06\u8655\u7406\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-183372","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183372","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=183372"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183372\/revisions"}],"predecessor-version":[{"id":183373,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183372\/revisions\/183373"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}