PHP:下载MySQL表

PHP:下载MySQL表,php,mysql,csv,Php,Mysql,Csv,我想下载一个MySQL表作为CSV文件,但我得到这个错误“允许内存大小为1342177280字节”。我想这是因为文件是先创建然后下载的。我如何才能让用户开始从0下载文件,在这种情况下,我想我不需要这么多内存 这是我目前的代码: $output = ""; $table = "export_table"; $sql = mysql_query("select * from $table"); $columns_total = mysql_n

我想下载一个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;
$output=”“;
$table=“导出表”;
$sql=mysql\u查询(“从$table中选择*);
$columns\u total=mysql\u num\u字段($sql);
//获取字段名
对于($i=0;$i<$columns_total;$i++){
$heading=mysql\u字段\u名称($sql,$i);
$output.='''.$heading'.''.'',';
}
$output.=“\n”;
//从表中获取记录
while($row=mysql\u fetch\u数组($sql)){
对于($i=0;$i<$columns_total;$i++){
$output.=''''.$row[“$i”].'”,';
}
$output.=“\n”;
}
//下载该文件
$filename=“export.csv”;
标题(“内容类型:应用程序/csv”);
标题('Content-Disposition:attachment;filename='。$filename);
echo$输出;

在遍历结果集之前,您必须移动头函数,而不是将其赋值给$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";
}
/。。。
$filename=“export.csv”;
标题(“内容类型:应用程序/csv”);
标题('Content-Disposition:attachment;filename='。$filename);
//只需将从数据库中获取的记录转储到输出中
while($row=mysql\u fetch\u数组($sql)){
$line='';
对于($i=0;$i<$columns_total;$i++){
$line.=''''.$row[“$i”].'',';
}
echo$行“\n”;
}
这可能会有问题,具体取决于您的具体情况,但是应该作为一个快速而肮脏的解决方案来工作。

使用以下代码:-

$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";

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

所以最后这就是我想要的:

$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);

代码的丑陋解决方案是ini_集(‘内存限制’,-1);它不会给出这个错误。搜索
MYSQL选择OUTFILE
然后给我一杯饮料,因为与您自己的方法相比,使用它后您会非常高兴:)另一个解决方案是在服务器上编写整个文件并允许使用它。首先,您必须将«我猜»转换为«我知道»。根据错误的生成方式,您可以应用其中一个建议。来自@Hanky的解决方案웃Panky在任何情况下都可能解决您的问题,但了解我们的代码如何失败很重要。另一个示例也可以做到这一点,对于需要解决此问题的人。