{"id":162823,"date":"2024-11-04T18:18:18","date_gmt":"2024-11-04T10:18:18","guid":{"rendered":"https:\/\/server.hk\/cnblog\/162823\/"},"modified":"2024-11-04T18:18:19","modified_gmt":"2024-11-04T10:18:19","slug":"%e6%95%99%e4%bd%a0%e5%a6%82%e4%bd%95%e5%be%9esql%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%8d%b2%e5%8f%96%e4%b8%80%e5%80%8b%e5%80%bc%ef%bc%88%e5%be%9esql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/162823\/","title":{"rendered":"\u6559\u4f60\u5982\u4f55\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u4e00\u500b\u503c\uff08\u5f9eSQL\u6578\u64da\u5eab\u8b80\u53d6\u4e00\u500b\u503c\uff09"},"content":{"rendered":"<h1 id=\"%e6%95%99%e4%bd%a0%e5%a6%82%e4%bd%95%e5%be%9esql%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%8d%b2%e5%8f%96%e4%b8%80%e5%80%8b%e5%80%bc%ef%bc%88%e5%be%9esql%e6%95%b8%e6%93%9a%e5%ba%ab%e8%ae%80%e5%8f%96-xpUQwQZepf\">\u6559\u4f60\u5982\u4f55\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u4e00\u500b\u503c\uff08\u5f9eSQL\u6578\u64da\u5eab\u8b80\u53d6\u4e00\u500b\u503c\uff09<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u6578\u64da\u9a45\u52d5\u6642\u4ee3\uff0cSQL\uff08\u7d50\u69cb\u5316\u67e5\u8a62\u8a9e\u8a00\uff09\u6210\u70ba\u4e86\u8207\u6578\u64da\u5eab\u9032\u884c\u4ea4\u4e92\u7684\u4e3b\u8981\u5de5\u5177\u4e4b\u4e00\u3002\u7121\u8ad6\u662f\u958b\u767c\u61c9\u7528\u7a0b\u5e8f\u9084\u662f\u9032\u884c\u6578\u64da\u5206\u6790\uff0c\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u7279\u5b9a\u7684\u503c\u90fd\u662f\u4e00\u9805\u57fa\u672c\u6280\u80fd\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u4e00\u500b\u503c\uff0c\u4e26\u63d0\u4f9b\u76f8\u95dc\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afsql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9f-xpUQwQZepf\">\u4ec0\u9ebc\u662fSQL\u6578\u64da\u5eab\uff1f<\/h2>\n<p>SQL\u6578\u64da\u5eab\u662f\u4e00\u7a2e\u4f7f\u7528SQL\u8a9e\u8a00\u4f86\u7ba1\u7406\u548c\u64cd\u4f5c\u6578\u64da\u7684\u6578\u64da\u5eab\u7cfb\u7d71\u3002\u5b83\u901a\u5e38\u7531\u591a\u500b\u8868\u7d44\u6210\uff0c\u6bcf\u500b\u8868\u5305\u542b\u591a\u884c\u548c\u591a\u5217\u7684\u6578\u64da\u3002SQL\u6578\u64da\u5eab\u7684\u5e38\u898b\u985e\u578b\u5305\u62ecMySQL\u3001PostgreSQL\u3001SQLite\u548cMicrosoft SQL Server\u7b49\u3002<\/p>\n<h2 id=\"%e5%be%9esql%e6%95%b8%e6%93%9a%e5%ba%ab%e4%b8%ad%e7%8d%b2%e5%8f%96%e5%80%bc%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%ad%a5%e9%a9%9f-xpUQwQZepf\">\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u503c\u7684\u57fa\u672c\u6b65\u9a5f<\/h2>\n<p>\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u4e00\u500b\u503c\u7684\u904e\u7a0b\u901a\u5e38\u5305\u62ec\u4ee5\u4e0b\u5e7e\u500b\u6b65\u9a5f\uff1a<\/p>\n<ul>\n<li>\u9023\u63a5\u5230\u6578\u64da\u5eab<\/li>\n<li>\u57f7\u884c\u67e5\u8a62<\/li>\n<li>\u7372\u53d6\u7d50\u679c<\/li>\n<li>\u95dc\u9589\u9023\u63a5<\/li>\n<\/ul>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e5%88%b0%e6%95%b8%e6%93%9a%e5%ba%ab-xpUQwQZepf\">1. \u9023\u63a5\u5230\u6578\u64da\u5eab<\/h3>\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\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\n$servername = \"localhost\";\n$username = \"username\";\n$password = \"password\";\n$dbname = \"database_name\";\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<h3 id=\"2-%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2-xpUQwQZepf\">2. \u57f7\u884c\u67e5\u8a62<\/h3>\n<p>\u9023\u63a5\u6210\u529f\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528SQL\u67e5\u8a62\u4f86\u7372\u53d6\u6240\u9700\u7684\u503c\u3002\u5047\u8a2d\u6211\u5011\u6709\u4e00\u500b\u540d\u70ba\u201cusers\u201d\u7684\u8868\uff0c\u4e26\u5e0c\u671b\u7372\u53d6\u7528\u6236ID\u70ba1\u7684\u7528\u6236\u540d\u7a31\uff1a<\/p>\n<pre><code>\n$sql = \"SELECT username FROM users WHERE id = 1\";\n$result = $conn-&gt;query($sql);\n<\/code><\/pre>\n<h3 id=\"3-%e7%8d%b2%e5%8f%96%e7%b5%90%e6%9e%9c-xpUQwQZepf\">3. \u7372\u53d6\u7d50\u679c<\/h3>\n<p>\u57f7\u884c\u67e5\u8a62\u5f8c\uff0c\u53ef\u4ee5\u6aa2\u67e5\u7d50\u679c\u4e26\u7372\u53d6\u6240\u9700\u7684\u503c\uff1a<\/p>\n<pre><code>\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6578\u64da\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"\u7528\u6236\u540d\u7a31: \" . $row[\"username\"];\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n<\/code><\/pre>\n<h3 id=\"4-%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-xpUQwQZepf\">4. \u95dc\u9589\u9023\u63a5<\/h3>\n<p>\u5b8c\u6210\u64cd\u4f5c\u5f8c\uff0c\u61c9\u8a72\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\u4ee5\u91cb\u653e\u8cc7\u6e90\uff1a<\/p>\n<pre><code>\n$conn-&gt;close();\n<\/code><\/pre>\n<h2 id=\"%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-xpUQwQZepf\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u503c\u6642\uff0c\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805\u9700\u8981\u8003\u616e\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fddSQL\u67e5\u8a62\u7684\u6b63\u78ba\u6027\uff0c\u4ee5\u907f\u514d\u8a9e\u6cd5\u932f\u8aa4\u3002<\/li>\n<li>\u4f7f\u7528\u53c3\u6578\u5316\u67e5\u8a62\u4f86\u9632\u6b62SQL\u6ce8\u5165\u653b\u64ca\u3002<\/li>\n<li>\u5728\u8655\u7406\u654f\u611f\u6578\u64da\u6642\uff0c\u61c9\u9075\u5faa\u6578\u64da\u4fdd\u8b77\u6cd5\u898f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-xpUQwQZepf\">\u7e3d\u7d50<\/h2>\n<p>\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u7372\u53d6\u4e00\u500b\u503c\u662f\u4e00\u9805\u57fa\u672c\u800c\u91cd\u8981\u7684\u6280\u80fd\uff0c\u7121\u8ad6\u662f\u5728\u958b\u767c\u61c9\u7528\u7a0b\u5e8f\u9084\u662f\u9032\u884c\u6578\u64da\u5206\u6790\u6642\u90fd\u6703\u7528\u5230\u3002\u901a\u904e\u672c\u6587\u4ecb\u7d39\u7684\u6b65\u9a5f\u548c\u793a\u4f8b\uff0c\u60a8\u61c9\u8a72\u80fd\u5920\u8f15\u9b06\u5730\u5f9e\u6578\u64da\u5eab\u4e2d\u8b80\u53d6\u6240\u9700\u7684\u503c\u3002\u5982\u679c\u60a8\u9700\u8981\u9ad8\u6548\u7684\u6578\u64da\u5b58\u5132\u548c\u7ba1\u7406\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u652f\u6301\u60a8\u7684\u696d\u52d9\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5f9eSQL\u6578\u64da\u5eab\u4e2d\u63d0\u53d6\u55ae\u4e00\u503c\uff0c\u638c\u63e1\u57fa\u672c\u67e5\u8a62\u6280\u5de7\uff0c\u63d0\u5347\u6578\u64da\u64cd\u4f5c\u80fd\u529b\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-162823","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162823","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=162823"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162823\/revisions"}],"predecessor-version":[{"id":162824,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/162823\/revisions\/162824"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=162823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=162823"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=162823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}