php 导出数据到csv文件
<?php
set_time_limit(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.time().'.csv');
$handle = fopen('php://output' ,'a');
$head = array('姓名' ,'性别' ,'年龄' ,'mail');
foreach ( $head as $k => $v )
{
$head[$k] = iconv('utf-8' ,'gb2312' ,$v);
}
fputcsv($handle ,$head);
$sql = 'SELECT * FROM `table_name`';
$stmt = mysql_query($sql);
$count = 0;
$limit = 100000;
while( $row = mysql_fetch_array($stmt ,MYSQL_ASSOC) ) {
$count ++;
if( $count == $limit )
{
ob_flush();
flush();
$count = 0;
}
foreach( $row as $k => $v ) {
$row[$k] = iconv('utf-8' ,'gb2312' ,$v);
}
fputcsv($handle ,$row);
}