php - 在真实主机上使用 UTF-8 将表数据导出到 CSV 文件
问题描述
我尝试将数据从 MySQL 表导出到 CSV 文件,它适用于我的虚拟主机但是当我在真实主机上上传它时,导出有 UTF-8 的问题,我使用下面的代码
require_once 'config.php';
$questions = questions::getAssoc("SELECT id,question FROM question");
download_csv_results($questions, 'file.csv');
exit();
function download_csv_results($questions , $name)
{
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;
filename=file.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-
check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
$outstream = fopen("php://output", "wb");
fputcsv($outstream, array_keys($questions[0]));
foreach($questions as $question)
{
$id = $question['id'];
fputcsv($outstream, $question);
$answers = answers::getAssoc("SELECT name,answer,mobile FROM answer WHERE qid=$id");
foreach ($answers as $answer){
fputcsv($outstream, $answer);
}
}
fclose($outstream);
}
解决方案
推荐阅读
- sql - SQL Server:内部联接运行总计
- postgresql - 通过创建数据源与 PostgreSQL 12 连接时出错
- list - 在 ocaml 中打印 int -> int list
- windows-10 - Windows 有时会将 msi 路径记录放在 \HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\ 中。如何禁用该选项?
- powershell - Azure 标记问题
- python - 序列与字母注释的“位置感知”对齐
- view - HANA CDS 视图 vs 计算视图 vs 表函数
- azure-language-understanding - 有谁知道 TimezoneOffset 对 LuisPredictionOptions 做了什么?
- typegraphql - 如何使用 type-graphql 和 RESTDataSource
- r - 用不同的 x 和 y 轴比例覆盖两个 ggplots