PHP 将MySQL数据导出为CSV

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 将MySQL数据导出为CSV相关的知识,希望对你有一定的参考价值。

$result = mysql_query("SELECT * FROM table ORDER BY column DESC");

// I hard-code the column names so I can capitalize, add spaces, etc.
$fields = '"User ID","Name","Email","Registration Date"'."\n";

// Iterate through the rows of data
while ($row = mysql_fetch_assoc($result))
{
	$fields .= '"'.$row['id'].'","'.$row['name'].'","'.$row['email'].'","'.$row['registration_date'].'"'."\n";
}

// Set our headers
header('Content-type: text/csv');
// To display data in-browser, change the header below to:
// header("Content-Disposition: inline");
header("Content-Disposition: attachment; filename=event_registrations.csv");

// Output our data
echo $fields;

以上是关于PHP 将MySQL数据导出为CSV的主要内容,如果未能解决你的问题,请参考以下文章

php 将MySQL导出为CSV(php脚本)

php 将MySQL导出为CSV(php脚本)

将mysql的查询结果导出为csv

使用 PHP/PDO 将大型 MySQL 表导出为 CSV

PHP 通过PHP将MYSQL导出为EXCEL / CSV

PHP 将MySQL查询结果导出为CSV