【发布时间】:2014-07-26 19:29:06
【问题描述】:
下面是我用来将 MySQL 数据库导出到 .csv 文件的脚本。
对于较小的输出(大约 30,000 行以下)似乎可以正常工作,但会挂在较大的输出上并且无法完成(总共 100 万行)。
有什么想法吗?
此外,PHPMyAdmin 导出实用程序可以非常快地转储它(只需几秒钟)。想知道它是如何做到的。
if(isset($_POST['submit'])) {
// Fetch Record from Database
$output = "";
$table = "tt_output";
$sql = mysqli_query($connection,"select * from {$table}");
$columns_total = mysqli_num_fields($sql);
// Get The Field Name
$query = "SHOW COLUMNS FROM {$table}"; $result_set = mysqli_query($connection,$query);
while ($result = mysqli_fetch_array($result_set)) {
$heading = $result[0];
$output .= trim($heading.',');
}
$output = substr($output,0,strlen($output)-1)."\r\n";
// Get Records from the table
while ($row = mysqli_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output = substr($output,0,strlen($output)-1)."\r\n";
}
// Download the file
$filename = "output".".csv";
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition: attachment; filename='.$filename);
header("Content-Transfer-Encoding: binary");
header('Connection: Keep-Alive');
echo $output;
exit;
}
【问题讨论】: