5

我正在尝试将 MySQLi 查询写入可下载的 CSV。以下标头为 CSV 打开一个流:

$fileName = ''; //empty file name, file name is cast later
header("Cache=Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen( 'php://output', 'w' );

在此之下,我有以下尝试查询并将查询的每一行转换为 CSV 文件中的新行:

if(isset($_POST["Submit"]) && ($_POST['Weight'] == 'Weight')){
    $fileName .= 'OutputWeightNull.csv';
    $query = mysqli_query($conn, 'SELECT * FROM  `Some_Table` WHERE WEIGHT = 0 OR weight IS NULL')or die(mysql_error());
    $result = mysqli_fetch_array($query) or die(mysql_error());
    $headerDisplayed = false;
    foreach ($result as $data){
    if (!headerDisplayed){
    fputcsv($fh, array_keys());
    $headerDisplayed = true;
        }
        fputcsv($fh, $data);
    }
}

CSV 正在根据需要下载到浏览器,但它显示为空,查询结果未发送到 CSV。谁能指出我为什么会这样。

这是我第一次尝试比 hello world 更复杂的 PHP 脚本。如果答案非常简单或显而易见,我们深表歉意。

4

3 回答 3

3

稍微清理一下代码以循环返回的行,并使用 mysqli_ 错误函数:-

<?php 

if(isset($_POST["Submit"]) && ($_POST['Weight'] == 'Weight'))
{
    $fileName = ''; //empty file name, file name is cast later
    header("Cache=Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Description: File Transfer');
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename={$fileName}");
    header("Expires: 0");
    header("Pragma: public");
    $fh = @fopen( 'php://output', 'w' );

    $fileName .= 'OutputWeightNull.csv';
    $query = mysqli_query($conn, 'SELECT * FROM  `Some_Table` WHERE WEIGHT = 0 OR weight IS NULL') or die(mysqli_error($conn));
    if ($result = mysqli_fetch_array($query, MYSQLI_ASSOC))
    {
        fputcsv($fh, array_keys($result));
        fputcsv($fh, $result);
        while ($result = mysqli_fetch_array($query, MYSQLI_ASSOC))
        {
            fputcsv($fh, $result);
        }
    }
}
于 2015-10-30T11:05:00.737 回答
2

MySQL 有一个内置的查询类型,用于将数据直接输出为 CSV 格式。

修改您的SELECT查询,使其采用以下格式:

SELECT fieldlist FROM table
INTO OUTFILE 'filename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

现在你不必做任何循环或类似的事情了;PHP 所要做的就是运行该查询,然后readfile()对 MySQL 生成的 CSV 文件执行一个操作,最后将其从磁盘中删除。

这将为您的 PHP 程序节省大量内存,并且运行速度也会更快(如果您有大量数据集,这将非常明显)。

于 2015-10-30T11:46:01.477 回答
2

像这样试试

if(isset($_POST["Submit"]) && ($_POST['Weight'] == 'Weight')){
 $fileName = 'OutputWeightNull.csv';
 $fh = @fopen( 'php://output', 'w' );
 header("Cache=Control: must-revalidate, post-check=0, pre-check=0");
 header('Content-Description: File Transfer');
 header("Content-type: text/csv");
 header("Content-Disposition: attachment; filename={$fileName}");
 header("Expires: 0");
 header("Pragma: public");

 $query = mysqli_query($conn, 'SELECT * FROM  `Some_Table` WHERE WEIGHT = 0 OR weight IS NULL')or die(mysql_error());
 $headerDisplayed = false;
 while($result = mysqli_fetch_array($query, MYSQLI_ASSOC)) {  
  if (!$headerDisplayed){
   fputcsv($fh, array_keys($result));
   $headerDisplayed = true;
  }
  fputcsv($fh, $result);
 }
}

您需要将 csv 标头放在“if”循环中

于 2015-10-30T10:38:46.880 回答