使用fputcsv导出mysql查询数组

时间:2015-06-15 03:49:52

标签: php mysql arrays csv fputcsv

我正在尝试使用fputcsv将sql查询的结果导出为CSV但是不断收到错误" fputcsv()期望参数2为数组,字符串为"。

我按照这里给出的答案 - 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);

2 个答案:

答案 0 :(得分:0)

对应official manual for mysqli_fetch_array

  

mysqli_fetch_array - 将结果行提取为关联行,数字数组或两者

你编码了MYSQLI_ASSOC标志,所以你得到一行数据的关联数组:

  

通过使用MYSQLI_ASSOC常量,此函数的行为与mysqli_fetch_assoc()相同

请参阅examples of mysqli_fetch_assoc以清楚了解我在谈论的内容。

所以要逐行将数据放到csv中,你应该组织循环,像这样:

<?
// 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);

答案 1 :(得分:0)

使用fputcsv导出MySQL查询数组

  

PHP fputcsv()

$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;
?>