简体   繁体   English

将MySQL导出为CSV

[英]Export MySQL to CSV

I have below script to export MySQL data to csv file.This script export everything inside the while loop but I want to put constant value in a 2nd column or just want to keep 2nd column empty and put MySQL data in column 1 and column 3. 我有下面的脚本将MySQL数据导出到csv文件中。此脚本导出while循环内的所有内容,但我想将常量值放在第二列中或只想让第二列为空并将MySQL数据放在第一列和第三列中。

// output the column headings
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

// fetch the data
$rows = mysql_query('SELECT field1,field2 FROM table');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);

You already have field1 (ie-Column1) and field2(ie-Column3) 您已经有field1(即,Column1)和field2(即,Column3)
so just add Null at 2nd position like below 所以只需在第二个位置添加Null,如下所示

$insert = array( '' );
array_splice( $row, 1, 0, $insert );  

then use 然后使用

fputcsv($output, $row);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM