{"id":96476,"date":"2024-10-20T09:46:17","date_gmt":"2024-10-20T01:46:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96476\/"},"modified":"2024-10-20T09:46:18","modified_gmt":"2024-10-20T01:46:18","slug":"php%e6%93%8d%e4%bd%9cmysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e9%80%a3%e6%8e%a5%e8%88%87%e5%a2%9e%e5%88%aa%e6%94%b9%e6%9f%a5-php%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96476\/","title":{"rendered":"PHP\u64cd\u4f5cMySQL\u6578\u64da\u5eab\uff1a\u9023\u63a5\u8207\u589e\u522a\u6539\u67e5 (php\u93c8\u63a5\u6578\u64da\u5eab\u64cd\u4f5c)"},"content":{"rendered":"<h1 id=\"php%e6%93%8d%e4%bd%9cmysql%e6%95%b8%e6%93%9a%e5%ba%ab%ef%bc%9a%e9%80%a3%e6%8e%a5%e8%88%87%e5%a2%9e%e5%88%aa%e6%94%b9%e6%9f%a5-php%e9%8f%88%e6%8e%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e6%93%8d%e4%bd%9c-HlAWSPYaWH\">PHP\u64cd\u4f5cMySQL\u6578\u64da\u5eab\uff1a\u9023\u63a5\u8207\u589e\u522a\u6539\u67e5 (php\u93c8\u63a5\u6578\u64da\u5eab\u64cd\u4f5c)<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0cPHP\u548cMySQL\u7684\u7d44\u5408\u662f\u6700\u5e38\u898b\u7684\u6280\u8853\u4e4b\u4e00\u3002\u9019\u7a2e\u7d44\u5408\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u8f15\u9b06\u5730\u5275\u5efa\u52d5\u614b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528PHP\u9023\u63a5MySQL\u6578\u64da\u5eab\uff0c\u4e26\u9032\u884c\u57fa\u672c\u7684\u589e\u3001\u522a\u3001\u6539\u3001\u67e5\u64cd\u4f5c\u3002<\/p>\n<h2 id=\"1-%e9%80%a3%e6%8e%a5mysql%e6%95%b8%e6%93%9a%e5%ba%ab-HlAWSPYaWH\">1. \u9023\u63a5MySQL\u6578\u64da\u5eab<\/h2>\n<p>\u5728\u958b\u59cb\u9032\u884c\u6578\u64da\u5eab\u64cd\u4f5c\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u8207MySQL\u6578\u64da\u5eab\u7684\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u7684mysqli\u64f4\u5c55\u4f86\u9023\u63a5\u6578\u64da\u5eab\u7684\u57fa\u672c\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nconnect_error) {\n    die(\"\u9023\u63a5\u5931\u6557: \" . $conn-&gt;connect_error);\n}\necho \"\u9023\u63a5\u6210\u529f\";\n?&gt;\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u4f7f\u7528mysqli\u985e\u4f86\u5275\u5efa\u4e00\u500b\u65b0\u7684\u6578\u64da\u5eab\u9023\u63a5\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8acb\u6839\u64da\u5be6\u969b\u60c5\u6cc1\u66ff\u63db\u4f3a\u670d\u5668\u5730\u5740\u3001\u7528\u6236\u540d\u3001\u5bc6\u78bc\u548c\u6578\u64da\u5eab\u540d\u7a31\u3002<\/p>\n<h2 id=\"2-%e5%a2%9e%e5%8a%a0%e6%95%b8%e6%93%9a-HlAWSPYaWH\">2. \u589e\u52a0\u6578\u64da<\/h2>\n<p>\u4e00\u65e6\u6210\u529f\u9023\u63a5\u5230\u6578\u64da\u5eab\uff0c\u5c31\u53ef\u4ee5\u9032\u884c\u6578\u64da\u7684\u63d2\u5165\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u63d2\u5165\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nquery($sql) === TRUE) {\n    echo \"\u65b0\u8a18\u9304\u63d2\u5165\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"<br>\" . $conn-&gt;error;\n}\n?&gt;\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5411\u540d\u70bausers\u7684\u8868\u4e2d\u63d2\u5165\u4e86\u4e00\u689d\u65b0\u8a18\u9304\u3002\u5982\u679c\u63d2\u5165\u6210\u529f\uff0c\u5c07\u986f\u793a\u76f8\u61c9\u7684\u63d0\u793a\u4fe1\u606f\u3002<\/p>\n<h2 id=\"3-%e6%9f%a5%e8%a9%a2%e6%95%b8%e6%93%9a-HlAWSPYaWH\">3. \u67e5\u8a62\u6578\u64da<\/h2>\n<p>\u67e5\u8a62\u6578\u64da\u662f\u6578\u64da\u5eab\u64cd\u4f5c\u4e2d\u6700\u5e38\u898b\u7684\u9700\u6c42\u4e4b\u4e00\u3002\u4ee5\u4e0b\u662f\u67e5\u8a62\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nquery($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\"]. \" - Name: \" . $row[\"username\"]. \" - Email: \" . $row[\"email\"]. \"<br>\";\n    }\n} else {\n    echo \"0 \u7d50\u679c\";\n}\n?&gt;\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u5f9eusers\u8868\u4e2d\u9078\u64c7\u6240\u6709\u7528\u6236\u7684id\u3001username\u548cemail\uff0c\u4e26\u5c07\u7d50\u679c\u8f38\u51fa\u5230\u7db2\u9801\u4e0a\u3002<\/p>\n<h2 id=\"4-%e6%9b%b4%e6%96%b0%e6%95%b8%e6%93%9a-HlAWSPYaWH\">4. \u66f4\u65b0\u6578\u64da<\/h2>\n<p>\u66f4\u65b0\u6578\u64da\u7684\u64cd\u4f5c\u4e5f\u975e\u5e38\u7c21\u55ae\uff0c\u4ee5\u4e0b\u662f\u66f4\u65b0\u6578\u64da\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>\nquery($sql) === TRUE) {\n    echo \"\u8a18\u9304\u66f4\u65b0\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"<br>\" . $conn-&gt;error;\n}\n?&gt;\n<\/code><\/pre>\n<p>\u5728\u9019\u6bb5\u4ee3\u78bc\u4e2d\uff0c\u6211\u5011\u5c07username\u70batestuser\u7684\u7528\u6236\u7684email\u66f4\u65b0\u70banewemail@example.com\u3002<\/p>\n<h2 id=\"5-%e5%88%aa%e9%99%a4%e6%95%b8%e6%93%9a-HlAWSPYaWH\">5. \u522a\u9664\u6578\u64da<\/h2>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u4f86\u770b\u770b\u5982\u4f55\u522a\u9664\u6578\u64da\uff1a<\/p>\n<pre><code>\nquery($sql) === TRUE) {\n    echo \"\u8a18\u9304\u522a\u9664\u6210\u529f\";\n} else {\n    echo \"\u932f\u8aa4: \" . $sql . \"<br>\" . $conn-&gt;error;\n}\n?&gt;\n<\/code><\/pre>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u522a\u9664username\u70batestuser\u7684\u7528\u6236\u8a18\u9304\u3002<\/p>\n<h2 id=\"6-%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-HlAWSPYaWH\">6. \u95dc\u9589\u9023\u63a5<\/h2>\n<p>\u5b8c\u6210\u6240\u6709\u64cd\u4f5c\u5f8c\uff0c\u8a18\u5f97\u95dc\u9589\u6578\u64da\u5eab\u9023\u63a5\uff1a<\/p>\n<pre><code>\nclose();\n?&gt;\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-HlAWSPYaWH\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528PHP\u9023\u63a5MySQL\u6578\u64da\u5eab\u4e26\u9032\u884c\u57fa\u672c\u7684\u589e\u3001\u522a\u3001\u6539\u3001\u67e5\u64cd\u4f5c\u3002\u9019\u4e9b\u64cd\u4f5c\u662f\u958b\u767c\u52d5\u614b\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5e8f\u7684\u57fa\u790e\u3002\u82e5\u60a8\u9700\u8981\u7a69\u5b9a\u7684\u6578\u64da\u5eab\u652f\u6301\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\u6258\u7ba1\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0c\u78ba\u4fdd\u9ad8\u6548\u80fd\u548c\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528PHP\u9023\u63a5MySQL\u6578\u64da\u5eab\uff0c\u638c\u63e1\u589e\u522a\u6539\u67e5\u64cd\u4f5c\uff0c\u8f15\u9b06\u7ba1\u7406\u6578\u64da\uff0c\u63d0\u5347\u7db2\u7ad9\u529f\u80fd\u6027\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-96476","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96476","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=96476"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96476\/revisions"}],"predecessor-version":[{"id":96477,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96476\/revisions\/96477"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}