set_time_limit(0);
error_reporting(0);

// 输出Excel文件头,可把user.csv换成你要的文件名
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: p_w_upload;filename="user.csv"');
header('Cache-Control: max-age=0');

$fp = fopen('/tmp/export/data.csv', 'a');
$head = array('UID', 'EMAIL', '注册时间', '第一次登陆时间');
foreach ($head as $i => $v) {
// CSV的Excel支持GBK编码,一定要转换,否则乱码
$head[$i] = iconv('utf-8', 'gbk', $v);
}
fputcsv($fp, $head);

$result = array(.................................);

foreach($resutl as $val){
ob_flush();
flush();

// 从数据库中获取数据,为了节省内存,不要把数据一次性读到内存,从句柄中一行一行读即可
$sql = "SELECT uid,email,regdate,lastlogintime FROM reg_members WHERE email in ($val) ORDER BY uid ASC, regdate ASC";
$query = $db_reg_read -> query($sql);
while ($res = $db_reg_read -> fetch_array($query)) {
$email = strtolower($res['email']);
$list[] = $res;
}

//对数据进行整理 

//输入到CSV中
foreach($list as $key => $val){
$row[0] = iconv('utf-8', 'gbk', $val['uid']);
$row[1] = iconv('utf-8', 'gbk', $val['email']);
$row[2] = iconv('utf-8', 'gbk', $val['regdate']);
$row[3] = iconv('utf-8', 'gbk', $val['lastlogintime']); 
fputcsv($fp, $row);

}