PHP:下载MySQL表

3
我希望下载一个MySQL表格作为CSV文件,但是我遇到了这个错误:“内存大小已耗尽1342177280字节”。我猜这是因为文件首先被创建,然后再下载。在这种情况下,我该如何实现用户可以从0开始下载文件,我认为这样我就不需要那么多内存。
这是目前我的代码:
$output         = "";
$table          = "export_table";
$sql            = mysql_query("select * from $table");
$columns_total  = mysql_num_fields($sql);

// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
    $heading    =   mysql_field_name($sql, $i);
    $output     .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
// Download the file
$filename =  "export.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;

你的代码的“丑陋解决方案”是ini_set('memory_limit',-1);它不会出现这个错误。 - Amit Shah
1
在SO上搜索“MYSQL SELECT INTO OUTFILE”,然后给我买一杯饮料,因为与你自己的方法相比,你会非常高兴使用它 :) - Hanky Panky
另一种解决方案是在服务器上编写整个文件,并允许用户下载。 - Amit Shah
首先,您必须将“我猜”转换为“我知道”。根据错误的生成方式,您可以应用其中一种建议。@Hanky웃Panky的解决方案可能会在任何情况下解决您的问题,但重要的是要知道我们的代码出了什么问题。 - fusion3k
3个回答

4

最终,这就是我想要的结果:

$db = mysqli_connect("localhost", "user", "password", "database");

$result = mysqli_query($db, "SELECT * FROM export_table", MYSQLI_USE_RESULT);

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=\"export_table.csv\"");
header("Content-Transfer-Encoding: binary");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);

$output = fopen('php://output', 'w');

    fputcsv($output, array('ID','Column1','Column2','Column3'));

while ($row = mysqli_fetch_assoc($result))
    {
    fputcsv($output, $row);
    }

fclose($output);
mysqli_free_result($result);
mysqli_close($db);

另一个例子也可以做到这一点。对于需要解决此问题的人,请参考http://code.stephenmorley.org/php/creating-downloadable-csv-files/。 - Nan Zhou

0

在遍历结果集之前,您需要将标题函数移到前面,并且不是分配给 $output 变量,而是直接输出它。

// ...

$filename =  "export.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

// Just dump the records you fetch from the db into output
while ($row = mysql_fetch_array($sql)) {
    $line = '';
    for ($i = 0; $i < $columns_total; $i++) {
        $line .= '"' . $row["$i"] . '",';
    }
    echo $line, "\n";
}

这可能存在问题,取决于您的具体情况,但应该作为一种快速而简单的解决方案。


0
请使用以下代码:-
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'dbName';
$tableName = 'tableName';
$mysqldump=exec('which mysqldump');   

$command = "$mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname $tableName > $tableName.sql";

exec($command);

如果您需要下载整个数据库,请将$command替换为以下行:

$command = "$mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname  > $dbName.sql";

希望这能对你有所帮助 :)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接