{"id":183960,"date":"2024-11-10T09:10:22","date_gmt":"2024-11-10T01:10:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183960\/"},"modified":"2024-11-10T09:10:23","modified_gmt":"2024-11-10T01:10:23","slug":"%e4%bd%bf%e7%94%a8php%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e4%b8%a6%e7%8d%b2%e5%be%97%e7%b5%90%e6%9e%9c-php%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183960\/","title":{"rendered":"\u4f7f\u7528PHP\u9023\u63a5\u6578\u64da\u5eab\u67e5\u8a62\u4e26\u7372\u5f97\u7d50\u679c (php\u9023\u63a5\u6578\u64da\u5eab\u67e5\u8a62)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8php%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%9f%a5%e8%a9%a2%e4%b8%a6%e7%8d%b2%e5%be%97%e7%b5%90%e6%9e%9c-CsWpnpjubN\">\u4f7f\u7528PHP\u9023\u63a5\u6578\u64da\u5eab\u67e5\u8a62\u4e26\u7372\u5f97\u7d50\u679c<\/h1>\n<p>\u5728\u73fe\u4eca\u7684\u7db2\u7d61\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\u7528\u65bc\u6578\u64da\u5eab\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528PHP\u9023\u63a5\u6578\u64da\u5eab\uff0c\u57f7\u884c\u67e5\u8a62\u4e26\u7372\u5f97\u7d50\u679c\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u7406\u89e3\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"1-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-CsWpnpjubN\">1. \u74b0\u5883\u6e96\u5099<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86PHP\u548c\u4e00\u500b\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\uff08\u5982MySQL\uff09\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u672c\u5730\u4f3a\u670d\u5668\u74b0\u5883\uff08\u5982XAMPP\u6216MAMP\uff09\u4f86\u9032\u884c\u958b\u767c\u548c\u6e2c\u8a66\u3002<\/p>\n<h2 id=\"2-%e9%80%a3%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab-CsWpnpjubN\">2. \u9023\u63a5\u6578\u64da\u5eab<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u4f7f\u7528PHP\u9023\u63a5\u5230\u6578\u64da\u5eab\u3002\u4ee5\u4e0b\u662f\u9023\u63a5MySQL\u6578\u64da\u5eab\u7684\u57fa\u672c\u4ee3\u78bc\uff1a<\/p>\n<pre><code>&lt;?php\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}\necho \"\u9023\u63a5\u6210\u529f\";\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86mysqli\u64f4\u5c55\u4f86\u5275\u5efa\u8207\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u8acb\u6839\u64da\u4f60\u7684\u6578\u64da\u5eab\u8a2d\u7f6e\u66ff\u63db\u76f8\u61c9\u7684\u8b8a\u91cf\u3002<\/p>\n<h2 id=\"3-%e5%9f%b7%e8%a1%8c%e6%9f%a5%e8%a9%a2-CsWpnpjubN\">3. \u57f7\u884c\u67e5\u8a62<\/h2>\n<p>\u9023\u63a5\u6210\u529f\u5f8c\uff0c\u6211\u5011\u53ef\u4ee5\u57f7\u884cSQL\u67e5\u8a62\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u67e5\u8a62\u793a\u4f8b\uff0c\u5f9e\u6578\u64da\u8868\u4e2d\u9078\u64c7\u6240\u6709\u8a18\u9304\uff1a<\/p>\n<pre><code>&lt;?php\n$sql = \"SELECT * FROM table_name\"; \/\/ \u66ff\u63db\u70ba\u4f60\u7684\u6578\u64da\u8868\u540d\u7a31\n$result = $conn-&gt;query($sql);\n\nif ($result-&gt;num_rows &gt; 0) {\n    \/\/ \u8f38\u51fa\u6bcf\u884c\u6578\u64da\n    while($row = $result-&gt;fetch_assoc()) {\n        echo \"id: \" . $row[\"id\"]. \" - \u540d\u7a31: \" . $row[\"name\"]. \"&lt;br&gt;\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n?&gt;<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528\u4e86query()\u65b9\u6cd5\u4f86\u57f7\u884cSQL\u67e5\u8a62\uff0c\u4e26\u4f7f\u7528fetch_assoc()\u65b9\u6cd5\u4f86\u7372\u53d6\u7d50\u679c\u96c6\u4e2d\u7684\u6bcf\u4e00\u884c\u6578\u64da\u3002<\/p>\n<h2 id=\"4-%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-CsWpnpjubN\">4. \u95dc\u9589\u9023\u63a5<\/h2>\n<p>\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>&lt;?php\n$conn-&gt;close();\n?&gt;<\/code><\/pre>\n<h2 id=\"5-%e9%8c%af%e8%aa%a4%e8%99%95%e7%90%86-CsWpnpjubN\">5. \u932f\u8aa4\u8655\u7406<\/h2>\n<p>\u5728\u5be6\u969b\u958b\u767c\u4e2d\uff0c\u932f\u8aa4\u8655\u7406\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528try-catch\u8a9e\u53e5\u4f86\u6355\u7372\u7570\u5e38\uff0c\u4e26\u9032\u884c\u76f8\u61c9\u7684\u8655\u7406\uff1a<\/p>\n<pre><code>&lt;?php\ntry {\n    $conn = new mysqli($servername, $username, $password, $dbname);\n    if ($conn-&gt;connect_error) {\n        throw new Exception(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n    }\n    \/\/ \u57f7\u884c\u67e5\u8a62...\n} catch (Exception $e) {\n    echo \"\u932f\u8aa4: \" . $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-CsWpnpjubN\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528PHP\u9023\u63a5\u6578\u64da\u5eab\u4e26\u57f7\u884c\u67e5\u8a62\u662f\u4e00\u500b\u57fa\u672c\u4f46\u91cd\u8981\u7684\u6280\u80fd\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u4f60\u53ef\u4ee5\u8f15\u9b06\u5730\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u57f7\u884c\u67e5\u8a62\u4e26\u7372\u53d6\u7d50\u679c\u3002\u7121\u8ad6\u662f\u958b\u767c\u5c0f\u578b\u61c9\u7528\u9084\u662f\u5927\u578b\u7cfb\u7d71\uff0c\u9019\u4e9b\u77e5\u8b58\u90fd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u4f60\u7684PHP\u61c9\u7528\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f\u6578\u64da\u5eab\u7ba1\u7406\u9084\u662f\u7db2\u7ad9\u6258\u7ba1\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u53ef\u9760\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528PHP\u9023\u63a5\u6578\u64da\u5eab\u9032\u884c\u67e5\u8a62\uff0c\u8f15\u9b06\u7372\u53d6\u7d50\u679c\uff0c\u5be6\u73fe\u6578\u64da\u64cd\u4f5c\u8207\u7ba1\u7406\u7684\u9ad8\u6548\u89e3\u6c7a\u65b9\u6848\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-183960","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183960","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=183960"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183960\/revisions"}],"predecessor-version":[{"id":183961,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183960\/revisions\/183961"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}