{"id":183944,"date":"2024-11-10T09:02:20","date_gmt":"2024-11-10T01:02:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183944\/"},"modified":"2024-11-10T09:02:21","modified_gmt":"2024-11-10T01:02:21","slug":"%e4%bd%bf%e7%94%a8php%e7%9a%84get%e6%96%b9%e6%b3%95%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bf%a1%e6%81%af-php-get%e6%96%b9%e5%bc%8f%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183944\/","title":{"rendered":"\u4f7f\u7528PHP\u7684GET\u65b9\u6cd5\u7372\u53d6\u6578\u64da\u5eab\u4fe1\u606f (php get\u65b9\u5f0f\u7372\u53d6\u6578\u64da\u5eab)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8php%e7%9a%84get%e6%96%b9%e6%b3%95%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e5%ba%ab%e4%bf%a1%e6%81%af-aFScGWoCys\">\u4f7f\u7528PHP\u7684GET\u65b9\u6cd5\u7372\u53d6\u6578\u64da\u5eab\u4fe1\u606f<\/h1>\n<p>\u5728\u7db2\u9801\u958b\u767c\u4e2d\uff0cPHP\u662f\u4e00\u7a2e\u5ee3\u6cdb\u4f7f\u7528\u7684\u4f3a\u670d\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u7279\u5225\u9069\u5408\u65bc\u6578\u64da\u5eab\u4ea4\u4e92\u3002\u4f7f\u7528GET\u65b9\u6cd5\u7372\u53d6\u6578\u64da\u5eab\u4fe1\u606f\u662f\u4e00\u7a2e\u5e38\u898b\u7684\u505a\u6cd5\uff0c\u5c24\u5176\u662f\u5728\u9700\u8981\u5f9e\u7528\u6236\u7aef\u7372\u53d6\u53c3\u6578\u4ee5\u67e5\u8a62\u6578\u64da\u6642\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528PHP\u7684GET\u65b9\u6cd5\u4f86\u7372\u53d6\u6578\u64da\u5eab\u4fe1\u606f\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"get%e6%96%b9%e6%b3%95%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-aFScGWoCys\">GET\u65b9\u6cd5\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>GET\u65b9\u6cd5\u662fHTTP\u5354\u8b70\u4e2d\u7684\u4e00\u7a2e\u8acb\u6c42\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u65bc\u5f9e\u4f3a\u670d\u5668\u7372\u53d6\u6578\u64da\u3002\u7576\u7528\u6236\u5728\u700f\u89bd\u5668\u4e2d\u8f38\u5165URL\u4e26\u63d0\u4ea4\u8868\u55ae\u6642\uff0cGET\u65b9\u6cd5\u6703\u5c07\u8868\u55ae\u6578\u64da\u9644\u52a0\u5230URL\u7684\u67e5\u8a62\u5b57\u7b26\u4e32\u4e2d\u3002\u9019\u4f7f\u5f97\u7528\u6236\u80fd\u5920\u901a\u904eURL\u76f4\u63a5\u8a2a\u554f\u7279\u5b9a\u7684\u8cc7\u6e90\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8get%e6%96%b9%e6%b3%95%e7%8d%b2%e5%8f%96%e6%95%b8%e6%93%9a%e7%9a%84%e6%ad%a5%e9%a9%9f-aFScGWoCys\">\u4f7f\u7528GET\u65b9\u6cd5\u7372\u53d6\u6578\u64da\u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e5%89%b5%e5%bb%bahtml%e8%a1%a8%e5%96%ae-aFScGWoCys\">1. \u5275\u5efaHTML\u8868\u55ae<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u4e00\u500bHTML\u8868\u55ae\u4f86\u6536\u96c6\u7528\u6236\u8f38\u5165\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u8868\u55ae\u793a\u4f8b\uff0c\u8a72\u8868\u55ae\u5141\u8a31\u7528\u6236\u8f38\u5165\u4e00\u500bID\u4f86\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u4fe1\u606f\uff1a<\/p>\n<pre><code>&lt;form action=\"fetch_data.php\" method=\"get\"&gt;\n    &lt;label for=\"id\"&gt;\u8f38\u5165ID:&lt;\/label&gt;\n    &lt;input type=\"text\" id=\"id\" name=\"id\"&gt;\n    &lt;input type=\"submit\" value=\"\u67e5\u8a62\"&gt;\n&lt;\/form&gt;<\/code><\/pre>\n<h3 id=\"2-%e8%99%95%e7%90%86get%e8%ab%8b%e6%b1%82-aFScGWoCys\">2. \u8655\u7406GET\u8acb\u6c42<\/h3>\n<p>\u7576\u7528\u6236\u63d0\u4ea4\u8868\u55ae\u5f8c\uff0c\u6578\u64da\u5c07\u901a\u904eGET\u65b9\u6cd5\u767c\u9001\u5230\u6307\u5b9a\u7684PHP\u6587\u4ef6\uff08\u5728\u672c\u4f8b\u4e2d\u70bafetch_data.php\uff09\u3002\u6211\u5011\u53ef\u4ee5\u4f7f\u7528PHP\u7684$_GET\u8d85\u5168\u5c40\u6578\u7d44\u4f86\u7372\u53d6\u7528\u6236\u8f38\u5165\u7684ID\uff1a<\/p>\n<pre><code>&lt;?php\nif (isset($_GET['id'])) {\n    $id = $_GET['id'];\n    \/\/ \u9032\u4e00\u6b65\u8655\u7406\u6578\u64da\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"3-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-aFScGWoCys\">3. \u9023\u63a5\u6578\u64da\u5eab<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PDO\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\ntry {\n    $pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');\n    $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n} catch (PDOException $e) {\n    echo '\u9023\u63a5\u5931\u6557: ' . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"4-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-aFScGWoCys\">4. \u67e5\u8a62\u6578\u64da<\/h3>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u7528\u6236\u63d0\u4f9b\u7684ID\u4f86\u67e5\u8a62\u6578\u64da\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u793a\u4f8b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>&lt;?php\nif (isset($_GET['id'])) {\n    $id = $_GET['id'];\n    $stmt = $pdo-&gt;prepare('SELECT * FROM your_table WHERE id = :id');\n    $stmt-&gt;execute(['id' =&gt; $id]);\n    $result = $stmt-&gt;fetch(PDO::FETCH_ASSOC);\n    \n    if ($result) {\n        echo '\u67e5\u8a62\u7d50\u679c: ' . htmlspecialchars($result['column_name']);\n    } else {\n        echo '\u672a\u627e\u5230\u8a72ID\u7684\u6578\u64da\u3002';\n    }\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e5%ae%89%e5%85%a8%e6%80%a7%e8%80%83%e9%87%8f-aFScGWoCys\">\u5b89\u5168\u6027\u8003\u91cf<\/h2>\n<p>\u5728\u4f7f\u7528GET\u65b9\u6cd5\u7372\u53d6\u6578\u64da\u6642\uff0c\u5b89\u5168\u6027\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u7531\u65bcGET\u8acb\u6c42\u7684\u6578\u64da\u6703\u986f\u793a\u5728URL\u4e2d\uff0c\u56e0\u6b64\u5bb9\u6613\u53d7\u5230SQL\u6ce8\u5165\u653b\u64ca\u3002\u70ba\u4e86\u9632\u6b62\u9019\u7a2e\u60c5\u6cc1\uff0c\u61c9\u59cb\u7d42\u4f7f\u7528\u9810\u8655\u7406\u8a9e\u53e5\uff08\u5982\u4e0a\u4f8b\u6240\u793a\uff09\u4f86\u57f7\u884c\u6578\u64da\u5eab\u67e5\u8a62\u3002\u6b64\u5916\uff0c\u5c0d\u7528\u6236\u8f38\u5165\u9032\u884c\u9069\u7576\u7684\u9a57\u8b49\u548c\u904e\u6ffe\u4e5f\u662f\u5fc5\u8981\u7684\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-aFScGWoCys\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528PHP\u7684GET\u65b9\u6cd5\u7372\u53d6\u6578\u64da\u5eab\u4fe1\u606f\u662f\u4e00\u500b\u7c21\u55ae\u800c\u6709\u6548\u7684\u904e\u7a0b\u3002\u901a\u904e\u5275\u5efaHTML\u8868\u55ae\u3001\u8655\u7406GET\u8acb\u6c42\u3001\u9023\u63a5\u6578\u64da\u5eab\u4ee5\u53ca\u67e5\u8a62\u6578\u64da\uff0c\u6211\u5011\u53ef\u4ee5\u8f15\u9b06\u5730\u5f9e\u6578\u64da\u5eab\u4e2d\u6aa2\u7d22\u6240\u9700\u7684\u4fe1\u606f\u3002\u70ba\u4e86\u78ba\u4fdd\u61c9\u7528\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\uff0c\u958b\u767c\u8005\u61c9\u8a72\u59cb\u7d42\u6ce8\u610f\u9632\u7bc4SQL\u6ce8\u5165\u7b49\u5b89\u5168\u5a01\u8105\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u53ef\u9760\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\u4ee5\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u670d\u52d9\u65e8\u5728\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u548c\u9ad8\u6548\u7684\u6027\u80fd\uff0c\u7121\u8ad6\u662f\u7db2\u7ad9\u6258\u7ba1\u9084\u662f\u61c9\u7528\u7a0b\u5e8f\u958b\u767c\uff0c\u5747\u80fd\u6eff\u8db3\u60a8\u7684\u8981\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528PHP\u7684GET\u65b9\u6cd5\u5f9e\u6578\u64da\u5eab\u7372\u53d6\u4fe1\u606f\uff0c\u7c21\u5316\u6578\u64da\u6aa2\u7d22\u904e\u7a0b\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\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-183944","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183944","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=183944"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183944\/revisions"}],"predecessor-version":[{"id":183945,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183944\/revisions\/183945"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}