{"id":96612,"date":"2024-10-20T10:54:19","date_gmt":"2024-10-20T02:54:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96612\/"},"modified":"2024-10-20T10:54:20","modified_gmt":"2024-10-20T02:54:20","slug":"php%e4%bd%bf%e7%94%a8mysql%e5%91%bd%e4%bb%a4%e5%b0%8e%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%96%b9%e6%b3%95-php-%e5%b0%8e%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84mysql%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96612\/","title":{"rendered":"php\u4f7f\u7528mysql\u547d\u4ee4\u5c0e\u5165\u6578\u64da\u5eab\u7684\u65b9\u6cd5 (php \u5c0e\u5165\u6578\u64da\u5eab\u7684mysql\u547d\u4ee4)"},"content":{"rendered":"<h1 id=\"php%e4%bd%bf%e7%94%a8mysql%e5%91%bd%e4%bb%a4%e5%b0%8e%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab%e7%9a%84%e6%96%b9%e6%b3%95-TMZpBosIRT\">PHP\u4f7f\u7528MySQL\u547d\u4ee4\u5c0e\u5165\u6578\u64da\u5eab\u7684\u65b9\u6cd5<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u7db2\u7d61\u958b\u767c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u7ba1\u7406\u548c\u64cd\u4f5c\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002PHP\u4f5c\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u670d\u52d9\u5668\u7aef\u8173\u672c\u8a9e\u8a00\uff0c\u7d93\u5e38\u8207MySQL\u6578\u64da\u5eab\u7d50\u5408\u4f7f\u7528\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528PHP\u4f86\u5c0e\u5165\u6578\u64da\u5eab\uff0c\u7279\u5225\u662f\u901a\u904eMySQL\u547d\u4ee4\u4f86\u5be6\u73fe\u9019\u4e00\u904e\u7a0b\u3002<\/p>\n<h2 id=\"%e7%82%ba%e4%bb%80%e9%ba%bc%e4%bd%bf%e7%94%a8mysql%e5%91%bd%e4%bb%a4%e5%b0%8e%e5%85%a5%e6%95%b8%e6%93%9a%e5%ba%ab-TMZpBosIRT\">\u70ba\u4ec0\u9ebc\u4f7f\u7528MySQL\u547d\u4ee4\u5c0e\u5165\u6578\u64da\u5eab<\/h2>\n<p>\u4f7f\u7528MySQL\u547d\u4ee4\u5c0e\u5165\u6578\u64da\u5eab\u7684\u4e3b\u8981\u539f\u56e0\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6548\u6027\uff1a\u76f4\u63a5\u4f7f\u7528\u547d\u4ee4\u884c\u53ef\u4ee5\u5feb\u901f\u5c0e\u5165\u5927\u91cf\u6578\u64da\u3002<\/li>\n<li>\u9748\u6d3b\u6027\uff1a\u53ef\u4ee5\u6839\u64da\u9700\u8981\u81ea\u5b9a\u7fa9\u5c0e\u5165\u904e\u7a0b\u3002<\/li>\n<li>\u81ea\u52d5\u5316\uff1a\u53ef\u4ee5\u5c07\u5c0e\u5165\u904e\u7a0b\u81ea\u52d5\u5316\uff0c\u6e1b\u5c11\u4eba\u5de5\u5e72\u9810\u3002<\/li>\n<\/ul>\n<h2 id=\"%e6%ba%96%e5%82%99%e5%b7%a5%e4%bd%9c-TMZpBosIRT\">\u6e96\u5099\u5de5\u4f5c<\/h2>\n<p>\u5728\u958b\u59cb\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u78ba\u4fdd\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u5df2\u5b89\u88ddPHP\u548cMySQL\u3002<\/li>\n<li>\u64c1\u6709\u4e00\u500b\u53ef\u7528\u7684\u6578\u64da\u5eab\u548c\u76f8\u61c9\u7684\u7528\u6236\u6b0a\u9650\u3002<\/li>\n<li>\u6e96\u5099\u597d\u8981\u5c0e\u5165\u7684\u6578\u64da\u6587\u4ef6\uff0c\u901a\u5e38\u662fCSV\u6216SQL\u683c\u5f0f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e4%bd%bf%e7%94%a8php%e5%9f%b7%e8%a1%8cmysql%e5%91%bd%e4%bb%a4-TMZpBosIRT\">\u4f7f\u7528PHP\u57f7\u884cMySQL\u547d\u4ee4<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528PHP\u57f7\u884cMySQL\u547d\u4ee4\u5c0e\u5165\u6578\u64da\u5eab\u7684\u57fa\u672c\u6b65\u9a5f\uff1a<\/p>\n<h3 id=\"1-%e9%80%a3%e6%8e%a5%e5%88%b0mysql%e6%95%b8%e6%93%9a%e5%ba%ab-TMZpBosIRT\">1. \u9023\u63a5\u5230MySQL\u6578\u64da\u5eab<\/h3>\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-%e6%ba%96%e5%82%99%e5%b0%8e%e5%85%a5%e7%9a%84sql%e6%96%87%e4%bb%b6-TMZpBosIRT\">2. \u6e96\u5099\u5c0e\u5165\u7684SQL\u6587\u4ef6<\/h3>\n<p>\u5047\u8a2d\u60a8\u6709\u4e00\u500b\u540d\u70ba <code>data.sql<\/code> \u7684\u6587\u4ef6\uff0c\u8a72\u6587\u4ef6\u5305\u542b\u4e86\u8981\u5c0e\u5165\u7684\u6578\u64da\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u8b80\u53d6\u8a72\u6587\u4ef6\uff1a<\/p>\n<pre><code>\n$sql = file_get_contents('data.sql');\n<\/code><\/pre>\n<h3 id=\"3-%e5%9f%b7%e8%a1%8c%e5%b0%8e%e5%85%a5%e5%91%bd%e4%bb%a4-TMZpBosIRT\">3. \u57f7\u884c\u5c0e\u5165\u547d\u4ee4<\/h3>\n<p>\u4f7f\u7528 <code>mysqli_multi_query()<\/code> \u51fd\u6578\u4f86\u57f7\u884c\u591a\u689dSQL\u8a9e\u53e5\uff1a<\/p>\n<pre><code>\nif ($conn-&gt;multi_query($sql)) {\n    do {\n        \/\/ \u7372\u53d6\u7b2c\u4e00\u500b\u7d50\u679c\u96c6\n        if ($result = $conn-&gt;store_result()) {\n            while ($row = $result-&gt;fetch_row()) {\n                \/\/ \u8655\u7406\u7d50\u679c\n            }\n            $result-&gt;free();\n        }\n    } while ($conn-&gt;next_result());\n} else {\n    echo \"\u5c0e\u5165\u5931\u6557: \" . $conn-&gt;error;\n}\n<\/code><\/pre>\n<h3 id=\"4-%e9%97%9c%e9%96%89%e9%80%a3%e6%8e%a5-TMZpBosIRT\">4. \u95dc\u9589\u9023\u63a5<\/h3>\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-TMZpBosIRT\">\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u4f7f\u7528PHP\u5c0e\u5165\u6578\u64da\u5eab\u6642\uff0c\u8acb\u6ce8\u610f\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fddSQL\u6587\u4ef6\u7684\u683c\u5f0f\u6b63\u78ba\uff0c\u907f\u514d\u8a9e\u6cd5\u932f\u8aa4\u3002<\/li>\n<li>\u6aa2\u67e5\u6578\u64da\u5eab\u7684\u5b57\u7b26\u96c6\u8a2d\u7f6e\uff0c\u4ee5\u9632\u6b62\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u5728\u5c0e\u5165\u5927\u91cf\u6578\u64da\u6642\uff0c\u8003\u616e\u4f7f\u7528\u4e8b\u52d9\u4f86\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-TMZpBosIRT\">\u7e3d\u7d50<\/h2>\n<p>\u4f7f\u7528PHP\u548cMySQL\u547d\u4ee4\u5c0e\u5165\u6578\u64da\u5eab\u662f\u4e00\u500b\u9ad8\u6548\u4e14\u9748\u6d3b\u7684\u904e\u7a0b\u3002\u901a\u904e\u4e0a\u8ff0\u6b65\u9a5f\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07\u6578\u64da\u5c0e\u5165\u5230\u60a8\u7684\u6578\u64da\u5eab\u4e2d\u3002\u7121\u8ad6\u60a8\u662f\u958b\u767c\u8005\u9084\u662f\u6578\u64da\u5eab\u7ba1\u7406\u54e1\uff0c\u638c\u63e1\u9019\u4e00\u6280\u80fd\u90fd\u5c07\u5c0d\u60a8\u7684\u5de5\u4f5c\u5927\u6709\u88e8\u76ca\u3002\u5982\u679c\u60a8\u9700\u8981\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u4f86\u652f\u6301\u60a8\u7684\u6578\u64da\u5eab\u64cd\u4f5c\uff0c\u8acb\u8003\u616e\u6211\u5011\u7684\u670d\u52d9\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u4e91\u4f3a\u670d\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u63d0\u4f9b\u6700\u4f73\u7684\u89e3\u6c7a\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528PHP\u548cMySQL\u547d\u4ee4\u5c0e\u5165\u6578\u64da\u5eab\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\uff0c\u63d0\u5347\u958b\u767c\u6548\u7387\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-96612","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96612","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=96612"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96612\/revisions"}],"predecessor-version":[{"id":96613,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96612\/revisions\/96613"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}