登录 |

dump一个表的正确方法

2012年04月18日 下午 53:13 | 作者:

set_time_limit(0);
header("Content-type: application/x-msexcel");
header('Content-Disposition: attachment; filename="'.$_SERVER['REQUEST_TIME'].'.xls"');

mysql_connect('localhost','root','');
mysql_select_db('test');

$fp = fopen('php://output', 'w');
// mysql_query会获取结果集缓存到PHP运行空间,以支持mysql_num_rows,mysql_data_seek,所以大结果集查询时,占有内存巨大
//$res = mysql_query('SELECT * FROM bigTable');
$res = mysql_unbuffered_query('SELECT * FROM bigTable');
while ($row = mysql_fetch_array($res, MYSQL_NUM)) {
    //echo implode("\t", $row), "\n";
    fputcsv($fp, $row);
}
mysql_free_result($res);
fclose($fp);