所以我從來沒有寫過一個csv文件,但總是從csv文件讀取。根據我讀過的一些例子,這是我嘗試過的。mysql結果寫入到csv文件的數組?
<?php
$newCsvData = array();
$getCats = "SELECT * FROM $termTax WHERE taxonomy = 'product_cat'";
$catresults = $wpdb->get_results($getCats);
foreach($catresults as $catresult) {
$newCsvData[] = $catresult->term_id;
}
$handle = fopen('export.csv', 'w');
foreach ($newCsvData as $line) {
fputcsv($handle, $line);
}
fclose($handle);
?>
我結束了錯誤說fputcsv()預計參數2爲陣列,在給定的字符串是該線
fputcsv($handle, $line);
如果我的var_dump($ catresult);我得到的行中的一切都如預期中的數組... 例
object(stdClass)#401 (6) { ["term_taxonomy_id"]=> string(3) "224" ["term_id"]=> string(2) "37" ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "0" } object(stdClass)#402 (6) { ["term_taxonomy_id"]=> string(2) "35" ["term_id"]=> string(2) "35" ["taxonomy"]=> string(11) "product_cat" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "1" }
我只是不知道怎麼寫陣列到CSV ...
如果我的var_dump($線)我得到字符串(2)「37」每個字符串和它的正確值。
我在正確的道路上嗎?提前致謝。
更新:所以我創建了一個空白的PHP文件在我的wordpress安裝的根目錄,並使用此代碼,它的作品發現...以及某些代碼下面固定的代碼...但事情是它不會從內部工作我正在開發的插件。
什麼樣的價值觀不'的var_dump($ newCsvDat)'返回? –
從代碼很明顯。它是數組,每個字段包含$ catresult-> term_id作爲字符串。 –