{"id":175293,"date":"2024-11-08T04:39:18","date_gmt":"2024-11-07T20:39:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/175293\/"},"modified":"2024-11-08T04:39:19","modified_gmt":"2024-11-07T20:39:19","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8php%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%9a%84%e6%89%80%e6%9c%89id%ef%bc%9f-php-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e6%89%80%e6%9c%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/175293\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528PHP\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709ID\uff1f (php \u67e5\u8a62\u6578\u64da\u5eab\u6240\u6709id)"},"content":{"rendered":"<h1 id=\"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8php%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%9a%84%e6%89%80%e6%9c%89id%ef%bc%9f-VvvPBqnXAz\">\u5982\u4f55\u4f7f\u7528PHP\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709ID\uff1f<\/h1>\n<p>\u5728\u958b\u767c\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u6642\uff0c\u7d93\u5e38\u9700\u8981\u5f9e\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u6578\u64da\u3002\u5c0d\u65bc\u8a31\u591a\u61c9\u7528\u4f86\u8aaa\uff0c\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709ID\u662f\u4e00\u500b\u5e38\u898b\u7684\u9700\u6c42\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528PHP\u4f86\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709ID\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u4ee3\u78bc\u3002<\/p>\n<h2 id=\"%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-VvvPBqnXAz\">\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u958b\u59cb\u67e5\u8a62\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u7684MySQLi\u64f4\u5c55\u4f86\u9023\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<pre><code>\n$servername = \"localhost\"; \/\/ \u6578\u64da\u5eab\u4f3a\u670d\u5668\u5730\u5740\n$username = \"username\"; \/\/ \u6578\u64da\u5eab\u7528\u6236\u540d\n$password = \"password\"; \/\/ \u6578\u64da\u5eab\u5bc6\u78bc\n$dbname = \"database_name\"; \/\/ \u6578\u64da\u5eab\u540d\u7a31\n\n\/\/ \u5275\u5efa\u9023\u63a5\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<\/code><\/pre>\n<h2 id=\"%e6%9f%a5%e8%a9%a2%e6%89%80%e6%9c%89id-VvvPBqnXAz\">\u67e5\u8a62\u6240\u6709ID<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u5c31\u53ef\u4ee5\u57f7\u884c\u67e5\u8a62\u4f86\u7372\u53d6\u6240\u6709ID\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba\u201cusers\u201d\u7684\u8868\uff0c\u4e26\u4e14\u8a72\u8868\u4e2d\u6709\u4e00\u500b\u540d\u70ba\u201cid\u201d\u7684\u5217\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0bSQL\u67e5\u8a62\u4f86\u7372\u53d6\u6240\u6709ID\uff1a<\/p>\n<pre><code>\n$sql = \"SELECT id FROM users\";\n$result = $conn-&gt;query($sql);\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u6aa2\u67e5\u67e5\u8a62\u662f\u5426\u6210\u529f\uff0c\u4e26\u8655\u7406\u7d50\u679c\uff1a<\/p>\n<pre><code>\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6bcf\u4e00\u884c\u7684ID\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"ID: \" . $row[\"id\"] . \"<br>\";\n    }\n} else {\n    echo \"\u6c92\u6709\u627e\u5230\u4efb\u4f55ID\";\n}\n<\/code><\/pre>\n<h2 id=\"%e9%97%9c%e9%96%89%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-VvvPBqnXAz\">\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5<\/h2>\n<p>\u5728\u5b8c\u6210\u67e5\u8a62\u5f8c\uff0c\u8a18\u5f97\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\uff0c\u4ee5\u91cb\u653e\u8cc7\u6e90\uff1a<\/p>\n<pre><code>\n$conn-&gt;close();\n<\/code><\/pre>\n<h2 id=\"%e5%ae%8c%e6%95%b4%e7%a4%ba%e4%be%8b-VvvPBqnXAz\">\u5b8c\u6574\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684PHP\u4ee3\u78bc\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709ID\uff1a<\/p>\n<pre><code>\nconnect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\n\n\/\/ \u67e5\u8a62\u6240\u6709ID\n$sql = \"SELECT id FROM users\";\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6bcf\u4e00\u884c\u7684ID\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"ID: \" . $row[\"id\"] . \"<br>\";\n    }\n} else {\n    echo \"\u6c92\u6709\u627e\u5230\u4efb\u4f55ID\";\n}\n\n\/\/ \u95dc\u9589\u9023\u63a5\n$conn-&gt;close();\n?&gt;\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-VvvPBqnXAz\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528PHP\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709ID\u662f\u4e00\u500b\u76f8\u5c0d\u7c21\u55ae\u7684\u904e\u7a0b\uff0c\u53ea\u9700\u5efa\u7acb\u6578\u64da\u5eab\u9023\u63a5\u3001\u57f7\u884c\u67e5\u8a62\u4e26\u8655\u7406\u7d50\u679c\u5373\u53ef\u3002\u9019\u7a2e\u65b9\u6cd5\u53ef\u4ee5\u61c9\u7528\u65bc\u5404\u7a2e\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u904b\u884c\u60a8\u7684\u61c9\u7528\u7a0b\u5f0f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u9069\u5408\u4e0d\u540c\u9700\u6c42\u7684\u7528\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528PHP\u67e5\u8a62\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709ID\uff0c\u7c21\u55ae\u6b65\u9a5f\u6559\u4f60\u8f15\u9b06\u7372\u53d6\u6578\u64da\u5eab\u4e2d\u7684\u6240\u6709\u8a18\u9304ID\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-175293","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175293","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=175293"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175293\/revisions"}],"predecessor-version":[{"id":175294,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/175293\/revisions\/175294"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=175293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=175293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=175293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}