我有下面的脚本将 MySQL 数据导出到 csv 文件。该脚本导出 while 循环内的所有内容,但我想将常量值放在第二列中,或者只想将第二列保留为空并将 MySQL 数据放在第 1 列中,第 3 栏。
// 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);
最佳答案
您已经有 field1(即 Column1)和 field2(即 Column3)
所以只需在第二个位置添加 Null 即可,如下所示
$insert = array( '' );
array_splice( $row, 1, 0, $insert );
然后使用
fputcsv($output, $row);
关于php - 将 MySQL 导出到 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38222741/