2015-06-15 97 views
0

我想用fputcsv將sql查詢的結果導出爲CSV,但不斷收到錯誤「fputcsv()期望參數2爲數組,字符串給定」。使用fputcsv導出mysql查詢數組

我遵循這裏給出的答案 - Query mysql and export data as CSV in PHP - 但它會拋出上面的錯誤。我知道$ val是一個字符串,並且不會工作,因爲fputcsv需要一個數組,但不知道如何將每一行放入csv。如果我將$ row替換爲$ val,那麼只會在csv中獲得結果的第一行,而不是循環。我知道我錯過了一些明顯的東西,只需要輕輕一點就可以告訴我它是什麼。代碼如下:

// MySQLi Query 
$result = mysqli_query($con, " 

SELECT d.order_id AS 'Order ID', 
m1.meta_value AS 'First Name', 
m2.meta_value AS 'Last Name', 
m3.meta_value AS 'Address1', 
m4.meta_value AS 'Address2', 
m5.meta_value AS 'City', 
m6.meta_value AS 'State', 
m7.meta_value AS 'Post Code', 
f1.meta_value AS 'SKU', 
d.order_item_name AS 'Product Name', 
e2.meta_value AS 'Quantity', 
m8.meta_value AS 'Tracking - Carrier', 
m9.meta_value AS 'Tracking - Code', 
m10.meta_value AS 'Tracking - Date', 
m11.meta_value AS 'Tracking - Picked' 

FROM dk_posts p 
LEFT JOIN dk_postmeta m1 ON p.id = m1.post_id 
AND m1.meta_key = '_shipping_first_name' 
LEFT JOIN dk_postmeta m2 ON p.id = m2.post_id 
AND m2.meta_key = '_shipping_last_name' 
LEFT JOIN dk_postmeta m3 ON p.id = m3.post_id 
AND m3.meta_key = '_shipping_address_1' 
LEFT JOIN dk_postmeta m4 ON p.id = m4.post_id 
AND m4.meta_key = '_shipping_address_2' 
LEFT JOIN dk_postmeta m5 ON p.id = m5.post_id 
AND m5.meta_key = '_shipping_city' 
LEFT JOIN dk_postmeta m6 ON p.id = m6.post_id 
AND m6.meta_key = '_shipping_state' 
LEFT JOIN dk_postmeta m7 ON p.id = m7.post_id 
AND m7.meta_key = '_shipping_postcode' 
LEFT JOIN dk_postmeta m8 ON p.id = m8.post_id 
AND m8.meta_key = 'ywot_carrier_name' 
LEFT JOIN dk_postmeta m9 ON p.id = m9.post_id 
AND m9.meta_key = 'ywot_tracking_code' 
LEFT JOIN dk_postmeta m10 ON p.id = m10.post_id 
AND m10.meta_key = 'ywot_pick_up_date' 
LEFT JOIN dk_postmeta m11 ON p.id = m11.post_id 
AND m11.meta_key = 'ywot_picked_up' 

LEFT JOIN dk_woocommerce_order_items d ON d.order_id=m1.post_id 

LEFT JOIN dk_woocommerce_order_itemmeta e1 ON d.order_item_id =  e1.order_item_id 
AND e1.meta_key = '_product_id' 
LEFT JOIN dk_woocommerce_order_itemmeta e2 ON d.order_item_id = e2.order_item_id 
AND e2.meta_key = '_qty' 

LEFT JOIN dk_postmeta f1 ON e1.meta_value = f1.post_id 
AND f1.meta_key = '_sku' 

WHERE post_status = 'wc-processing' 
AND 
order_item_type = 'line_item' 
"); 

$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 

$fp = fopen('file.csv', 'w'); 

foreach ($row as $val) { 
    fputcsv($fp, $val); 
} 

fclose($fp); 

回答

0

對應official manual for mysqli_fetch_array

mysqli_fetch_array - 從結果集中取得一行作爲關聯,數字數組,或二者

您編碼MYSQLI_ASSOC標誌,所以喲獲得關聯數組一行的數據:

通過使用MYSQLI_ASSOC不變這一功能將相同的行爲與mysqli_fetch_assoc()

examples of mysqli_fetch_assoc清楚地明白我在說什麼。

所以由行把數據爲csv行,U應組織循環,水木清華這樣的:

<? 
// making your query 

$fp = fopen('file.csv', 'w'); 
$placed_header = false; 
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
    // add header to table 
    if(!$placed_header) { 
     fputcsv($fp, array_keys($row)); 
     $placed_header = true; 
    } 

    // place row of data 
    fputcsv($fp, array_values($row)); 
} 

fclose($fp); 
+0

我試過這個,並且拋出了一個新的錯誤: 警告:array_keys()期望參數1是數組,在第75行的/home/example/export/export3.php中給出的null參數1 警告:fputcsv()預計參數2是數組,在第75行的/home/example/export/export3.php中給出null null 警告:array_values()期望參數1是數組,在/home/example/export/export3.php中給出null 80 警告:fputcsv()期望參數2是數組,在第80行上的/home/example/export/export3.php中給出的爲null ...等等。 – d1ch0t0my

+0

已修復。將循環中的$ val替換爲實際循環的數據,即$ row。 – userlond

+0

完美。謝謝你的幫助。我現在也瞭解mysqli_fetch_assoc的差異,所以我的教育進展是:)。再次感謝。 – d1ch0t0my

0

使用fputcsv

PHP fputcsv出口MySQL查詢陣列()

$filename = "toy_csv.csv"; 
$fp = fopen('php://output', 'w'); 

$query = "$result = mysqli_query($con, " 

SELECT d.order_id AS 'Order ID', 
m1.meta_value AS 'First Name', 
m2.meta_value AS 'Last Name', 
m3.meta_value AS 'Address1', 
m4.meta_value AS 'Address2', 
m5.meta_value AS 'City', 
m6.meta_value AS 'State', 
m7.meta_value AS 'Post Code', 
f1.meta_value AS 'SKU', 
d.order_item_name AS 'Product Name', 
e2.meta_value AS 'Quantity', 
m8.meta_value AS 'Tracking - Carrier', 
m9.meta_value AS 'Tracking - Code', 
m10.meta_value AS 'Tracking - Date', 
m11.meta_value AS 'Tracking - Picked' 

FROM dk_posts p 
LEFT JOIN dk_postmeta m1 ON p.id = m1.post_id 
AND m1.meta_key = '_shipping_first_name' 
LEFT JOIN dk_postmeta m2 ON p.id = m2.post_id 
AND m2.meta_key = '_shipping_last_name' 
LEFT JOIN dk_postmeta m3 ON p.id = m3.post_id 
AND m3.meta_key = '_shipping_address_1' 
LEFT JOIN dk_postmeta m4 ON p.id = m4.post_id 
AND m4.meta_key = '_shipping_address_2' 
LEFT JOIN dk_postmeta m5 ON p.id = m5.post_id 
AND m5.meta_key = '_shipping_city' 
LEFT JOIN dk_postmeta m6 ON p.id = m6.post_id 
AND m6.meta_key = '_shipping_state' 
LEFT JOIN dk_postmeta m7 ON p.id = m7.post_id 
AND m7.meta_key = '_shipping_postcode' 
LEFT JOIN dk_postmeta m8 ON p.id = m8.post_id 
AND m8.meta_key = 'ywot_carrier_name' 
LEFT JOIN dk_postmeta m9 ON p.id = m9.post_id 
AND m9.meta_key = 'ywot_tracking_code' 
LEFT JOIN dk_postmeta m10 ON p.id = m10.post_id 
AND m10.meta_key = 'ywot_pick_up_date' 
LEFT JOIN dk_postmeta m11 ON p.id = m11.post_id 
AND m11.meta_key = 'ywot_picked_up' 

LEFT JOIN dk_woocommerce_order_items d ON d.order_id=m1.post_id 

LEFT JOIN dk_woocommerce_order_itemmeta e1 ON d.order_item_id =  e1.order_item_id 
AND e1.meta_key = '_product_id' 
LEFT JOIN dk_woocommerce_order_itemmeta e2 ON d.order_item_id = e2.order_item_id 
AND e2.meta_key = '_qty' 

LEFT JOIN dk_postmeta f1 ON e1.meta_value = f1.post_id 
AND f1.meta_key = '_sku' 

WHERE post_status = 'wc-processing' 
AND 
order_item_type = 'line_item' 
"); 
$row = mysqli_fetch_array($result, MYSQLI_ASSOC); 
while ($row = mysql_fetch_row($result)) { 
    $header[] = $row[0]; 
} 
header('Content-type: application/csv'); 
header('Content-Disposition: attachment; filename='.$filename); 
fputcsv($fp, $header); 

$num_column = count($header);  
$query = "SELECT * FROM toy"; 
$result = mysqli_query($query); 
while($row = mysqli_fetch_row($result)) { 
    fputcsv($fp, $row); 
} 
exit; 
?>