PHP-将MySQL查询导出到CSV Changeup

PHP-将MySQL查询导出到CSV Changeup,php,mysql,csv,fputcsv,Php,Mysql,Csv,Fputcsv,我看到很多关于导出到CSV的正确方法的帖子,大多数开发人员建议使用fputcsv() 如何将下面的脚本转换为使用fputcsv? 您将看到我还导出了标题行,它反映了表列名,我希望保留它 <?php $sql = "SELECT * FROM `tbl_customers`"; $result = mysql_query($sql, $dbdata_conn) or die(mysql_error()); $header = $csv_output = '';

我看到很多关于导出到CSV的正确方法的帖子,大多数开发人员建议使用
fputcsv()

如何将下面的脚本转换为使用
fputcsv
? 您将看到我还导出了标题行,它反映了表列名,我希望保留它

<?php

    $sql = "SELECT * FROM `tbl_customers`";
    $result = mysql_query($sql, $dbdata_conn) or die(mysql_error());


    $header = $csv_output = '';
    $fields = mysql_num_fields($result);
    for ($i = 0; $i < $fields; $i++) {
        $header .= mysql_field_name($result, $i) . ",";
    }
    $header .= "\n";

    while ($rowr = mysql_fetch_row($result)) {
      for ($j=0; $j<$i; $j++) {
        $csv_output .= $rowr[$j].", ";
          }
     $csv_output .= "\n";
    }

    $csv_output = $header.$csv_output;

    header("Content-type: text/x-csv");
    header("Content-Disposition: attachment; filename=test.csv");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$csv_output";
    exit;   

?>

我知道,
mysql\u query
已被弃用,因此这是为了练习

顺便说一句,我不熟悉
fputcsv
,但我读到它对于格式化csv输出的数据非常有用,可以节省我们所有转义等操作的时间。 (我也非常愿意对上述内容进行改进)

简单演示(遵循您的
mysql.*
函数):

$header=array();
$fields=mysql\u num\u字段($result);
对于($i=0;$i<$fields;$i++){
$header[]=mysql\u field\u name($result,$i);
}
标题(“…”);
$f=fopen(“php://output“,“wt”);
fputcsv($f,$header);
while($row=mysql\u fetch\u row($result)){
fputcsv($f,$row);
}
外国法郎(f美元);

正如您所说,
mysql\uquot.*
函数已被弃用,因此您也应该使用它。

如果您想将其作为附件下载,可以不使用,但如果您想使用它,这里有一个解决方法:

$sql = "SELECT * FROM `tbl_customers`";
$result = mysql_query($sql, $dbdata_conn) or die(mysql_error());

$header = array();
$csv_output = array();
$fields = mysql_num_fields($result);
for ($i = 0; $i < $fields; $i++) {
    $header[] = mysql_field_name($result, $i);
}
$csv_output[] = $header;

while ($rowr = mysql_fetch_array($result)) {
    $csv_output[] = $rowr;
}

$fp = fopen('/path/to/file.csv', 'w');  
foreach ($csv_output as $line) {
    fputcsv($fp, $line);
}
fclose($fp);    

// if you pick up the file from the directory manually, the following is not needed
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=test.csv");
header("Pragma: no-cache");
header("Expires: 0");

print file_get_contents('/path/to/file.csv');
unlink('/path/to/file.csv');
exit;   
$sql=“从“tbl_客户”中选择*”;
$result=mysql\u query($sql,$dbdata\u conn)或die(mysql\u error());
$header=array();
$csv_输出=数组();
$fields=mysql\u num\u字段($result);
对于($i=0;$i<$fields;$i++){
$header[]=mysql\u field\u name($result,$i);
}
$csv_输出[]=$header;
while($rowr=mysql\u fetch\u数组($result)){
$csv_输出[]=$rowr;
}
$fp=fopen('/path/to/file.csv',w');
foreach($csv\u输出为$line){
fputcsv($fp,$line);
}
fclose($fp);
//如果手动从目录中提取文件,则不需要以下内容
标题(“内容类型:文本/x-csv”);
标题(“内容配置:附件;文件名=test.csv”);
标题(“杂注:无缓存”);
标题(“到期日:0”);
打印文件获取内容('/path/to/file.csv');
取消链接('/path/to/file.csv');
出口
你不想用你可以用我认为你想做的事
$sql = "SELECT * FROM `tbl_customers`";
$result = mysql_query($sql, $dbdata_conn) or die(mysql_error());

$header = array();
$csv_output = array();
$fields = mysql_num_fields($result);
for ($i = 0; $i < $fields; $i++) {
    $header[] = mysql_field_name($result, $i);
}
$csv_output[] = $header;

while ($rowr = mysql_fetch_array($result)) {
    $csv_output[] = $rowr;
}

$fp = fopen('/path/to/file.csv', 'w');  
foreach ($csv_output as $line) {
    fputcsv($fp, $line);
}
fclose($fp);    

// if you pick up the file from the directory manually, the following is not needed
header("Content-type: text/x-csv");
header("Content-Disposition: attachment; filename=test.csv");
header("Pragma: no-cache");
header("Expires: 0");

print file_get_contents('/path/to/file.csv');
unlink('/path/to/file.csv');
exit;