我在MySQL数据库中有数据。我向用户发送一个URL,以获取他们的数据作为CSV文件。

我有链接的电子邮件,MySQL查询等覆盖。

当他们点击链接时,如何弹出从MySQL下载带有记录的CVS的窗口?

我已经有了所有能拿到唱片的资料。我只是不明白如何让PHP创建CSV文件,并让他们下载一个扩展名为. CSV的文件。


当前回答

您可以使用fputcsv函数简单地将数据写入CSV。让我们看看下面的例子。将列表数组写入CSV文件

$list[] = array("Cars", "Planes", "Ships");
$list[] = array("Car's2", "Planes2", "Ships2");
//define headers for CSV 
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=file_name.csv');
//write data into CSV
$fp = fopen('php://output', 'wb');
//convert data to UTF-8 
fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
foreach ($list as $line) {
    fputcsv($fp, $line);
}
fclose($fp);

其他回答

〇简单的方法

$data = array (
    'aaa,bbb,ccc,dddd',
    '123,456,789',
    '"aaa","bbb"');

$fp = fopen('data.csv', 'wb');
foreach($data as $line){
    $val = explode(",",$line);
    fputcsv($fp, $val);
}
fclose($fp);

因此$data数组的每一行都将转到新创建的CSV文件的新行。它只适用于PHP 5及以后的版本。

嘿它工作得很好....!!!!谢谢Peter Mortensen和Connor Burton

<?php
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

ini_set('display_errors',1);
$private=1;
error_reporting(E_ALL ^ E_NOTICE);

mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$start = $_GET["start"];
$end = $_GET["end"];

$query = "SELECT * FROM customers WHERE created>='{$start} 00:00:00'  AND created<='{$end} 23:59:59'   ORDER BY id";
$select_c = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($select_c, MYSQL_ASSOC))
{
    $result.="{$row['email']},";
    $result.="\n";
    echo $result;
}

?>

header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");

function outputCSV($data) {
  $output = fopen("php://output", "wb");
  foreach ($data as $row)
    fputcsv($output, $row); // here you can change delimiter/enclosure
  fclose($output);
}

outputCSV(array(
  array("name 1", "age 1", "city 1"),
  array("name 2", "age 2", "city 2"),
  array("name 3", "age 3", "city 3")
));

php: / /输出 函数

公共函数actionExportnotificationresponselogdata() {

    $fileName = '/tmp/notificationresponselogs_' . date('d-m-Y-g-i-h') . '.csv';
    $f = fopen($fileName, 'w'); 
     fputs( $f, "\xEF\xBB\xBF" ); //for utf8 support in csv



        $csv_fields=array();
        $csv_fields[] = 'heading1';
        $csv_fields[] = 'heading2';
        $csv_fields[] = 'heading3';
        $csv_fields[] = 'heading4';
        $csv_fields[] = 'heading5';
        $csv_fields[] = 'heading6';
        $csv_fields[] = 'heading7';
        $csv_fields[] = 'heading8';
        fputcsv($f, $csv_fields);

    
$notification_log_arr = $notificationObj->getNotificationResponseForExport($params); //result from database

    if (count($notification_log_arr) > 0) {
        $serialNumber=1;
    foreach ($notification_log_arr AS $notifaction) {
            

            $fields = array();
            $fields['serialNumber']= $serialNumber  ;
            $fields['fld_1']= $notifaction['fld_1'] ;
            $fields['fld_2']= $notifaction['fld_2'] ;
            $fields['fld_3']= $notifaction['fld_3'] ;
            $fields['fld_4']= $notifaction['fld_4'] ;
            $fields['fld_5']= $notifaction['fld_5'] ;
            $fields['fld_6']= $notifaction['fld_6'] ; 
            $fields['fld_7']= $notifaction['fld_7'] ; 
           // print_r($fields); die;
            fputcsv($f, $fields,",");
        
            $serialNumber++;  }
            fclose($f);
            if (file_exists($fileName)) {
                
                header("Content-type: application/csv");
                header("Content-Disposition: attachment; filename="."exportlog".date("Y-m-d_H:i").".csv");
                header("Content-length: " . filesize($fileName));
                header("Pragma: no-cache"); 
                header("Expires: 0");
                readfile($fileName);
                unlink($fileName);
                exit;
            }
}

我知道这个帖子有点老了,但作为将来的参考,以及像我这样的新手:

这里的其他人都解释了如何创建CSV,但忽略了问题的基本部分:如何链接。为了链接到CSV-file的下载,只需链接到.php-file,该文件将响应为. CSV-file。PHP头文件可以做到这一点。这可以实现一些很酷的东西,比如向查询字符串中添加变量并自定义输出:

<a href="my_csv_creator.php?user=23&amp;othervariable=true">Get CSV</a>

my_csv_creator.php可以使用查询字符串中给出的变量,例如使用不同的或自定义的数据库查询,更改CSV的列,个性化文件名等,例如:

User_John_Doe_10_Dec_11.csv