{"id":29,"date":"2023-12-02T16:06:06","date_gmt":"2023-12-02T08:06:06","guid":{"rendered":"https:\/\/www.xhome.club\/?p=29"},"modified":"2023-12-02T16:07:34","modified_gmt":"2023-12-02T08:07:34","slug":"php%e5%af%bc%e5%85%a5csv%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/www.xhome.club\/index.php\/2023\/12\/02\/php%e5%af%bc%e5%85%a5csv%e6%96%87%e4%bb%b6\/","title":{"rendered":"PHP\u5bfc\u5165CSV\u6587\u4ef6"},"content":{"rendered":"<p>\u767e\u5ea6\u641c\u7d22\u5982\u4f55\u7528PHP\u5bfc\u5165CSV\u6587\u4ef6\uff0c\u7b2c\u4e00\u6761\u5f39\u51fa\u7684\u7adf\u7136\u662fAI\u751f\u6210\u7684\u7b54\u6848\uff0c\u5e76\u7ed9\u51fa\u4e86\u53ef\u590d\u5236\u7684\u4ee3\u7801\uff0c\u60ca\u4e0d\u60ca\u559c\uff1f\uff01<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528PHP\u7684\u5185\u7f6e\u51fd\u6570<code class=\"dqa-code-span\">fgetcsv()<\/code>\u6765\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u5c06\u5176\u4e2d\u7684\u6570\u636e\u5b58\u5165\u5230\u6570\u636e\u5e93\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<p>&lt;?php<br \/>\n\/\/ \u8fde\u63a5\u6570\u636e\u5e93<br \/>\n$servername = &#8220;localhost&#8221;; \/\/ \u670d\u52a1\u5668\u540d\u79f0\u6216IP\u5730\u5740<br \/>\n$username = &#8220;root&#8221;; \/\/ MySQL\u7528\u6237\u540d<br \/>\n$password = &#8220;&#8221;; \/\/ MySQL\u5bc6\u7801<br \/>\n$dbname = &#8220;myDB&#8221;; \/\/ \u8981\u64cd\u4f5c\u7684\u6570\u636e\u5e93\u540d\u79f0<\/p>\n<p>$conn = new mysqli($servername, $username, $password, $dbname);<br \/>\nif ($conn-&gt;connect_error) {<br \/>\ndie(&#8220;\u8fde\u63a5\u5931\u8d25: &#8221; . $conn-&gt;connect_error);<br \/>\n}<br \/>\necho &#8220;\u6210\u529f\u8fde\u63a5\u5230MySQL&lt;br&gt;&#8221;;<\/p>\n<p>\/\/ CSV\u6587\u4ef6\u8def\u5f84<br \/>\n$filepath = &#8216;data.csv&#8217;;<\/p>\n<p>\/\/ \u6253\u5f00CSV\u6587\u4ef6<br \/>\n$handle = fopen($filepath, &#8216;r&#8217;);<br \/>\nif (!$handle) {<br \/>\necho &#8220;\u65e0\u6cd5\u6253\u5f00CSV\u6587\u4ef6\uff01&#8221;;<br \/>\nexit;<br \/>\n}<\/p>\n<p>while (($rowData = fgetcsv($handle)) !== FALSE) {<br \/>\n\/\/ \u83b7\u53d6\u6bcf\u884c\u6570\u636e<br \/>\n$column1 = $rowData[0]; \/\/ \u7b2c\u4e00\u5217\u6570\u636e<br \/>\n$column2 = $rowData[1]; \/\/ \u7b2c\u4e8c\u5217\u6570\u636e<\/p>\n<p>\/\/ \u6784\u5efaINSERT\u8bed\u53e5<br \/>\n$sql = &#8220;INSERT INTO tableName (column1, column2) VALUES (&#8216;&#8221; . $column1 . &#8220;&#8216;, &#8216;&#8221; . $column2 . &#8220;&#8216;)&#8221;;<\/p>\n<p>if ($conn-&gt;query($sql) === TRUE) {<br \/>\necho &#8220;\u8bb0\u5f55\u5df2\u6dfb\u52a0\u5230\u6570\u636e\u5e93&lt;br&gt;&#8221;;<br \/>\n} else {<br \/>\necho &#8220;Error: &#8221; . $sql . &#8220;&lt;br&gt;&#8221; . $conn-&gt;error;<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ \u5173\u95edCSV\u6587\u4ef6\u548c\u6570\u636e\u5e93\u8fde\u63a5<br \/>\nfclose($handle);<br \/>\n$conn-&gt;close();<br \/>\n?&gt;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u767e\u5ea6\u641c\u7d22\u5982\u4f55\u7528PHP\u5bfc\u5165CSV\u6587\u4ef6\uff0c\u7b2c\u4e00\u6761\u5f39\u51fa\u7684\u7adf\u7136\u662fAI\u751f\u6210\u7684\u7b54\u6848\uff0c\u5e76\u7ed9\u51fa\u4e86\u53ef\u590d\u5236\u7684\u4ee3\u7801\uff0c\u60ca\u4e0d\u60ca\u559c\uff1f\uff01 \u53ef\u4ee5&hellip; <a class=\"more-link\" href=\"https:\/\/www.xhome.club\/index.php\/2023\/12\/02\/php%e5%af%bc%e5%85%a5csv%e6%96%87%e4%bb%b6\/\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">PHP\u5bfc\u5165CSV\u6587\u4ef6<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-29","post","type-post","status-publish","format-aside","hentry","category-it","post_format-post-format-aside","entry"],"_links":{"self":[{"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/posts\/29","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xhome.club\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}