【问题标题】:Export data from mysql with unicode characters使用 unicode 字符从 mysql 导出数据
【发布时间】:2016-10-30 08:32:13
【问题描述】:
<?php
include('db.php');

//header to give the order to the browser
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=exported-data.csv');

//select table to export the data
$select_table=mysql_query('select * from User');
$rows = mysql_fetch_assoc($select_table);

if ($rows)
{
getcsv(array_keys($rows));
}
while($rows)
{
getcsv($rows);
$rows = mysql_fetch_assoc($select_table);
}

// get total number of fields present in the database
function getcsv($no_of_field_names)
{
$separate = '';


// do the action for all field names as field name
foreach ($no_of_field_names as $field_name)
{
if (preg_match('/\\r|\\n|,|"/', $field_name))
{
$field_name = '' . str_replace('', $field_name) . '';
}
echo $separate . $field_name;

//sepearte with the comma
$separate = ',';
}

//make new row and line
echo "\r\n";
}
?>

您好,请帮我解决 Unicode 问题。上面的这个脚本运行良好,它导出了我需要的所有内容,但是所有用 Unicode 编写的数据都导出了:“??????”

谁能帮我修改这个脚本?

谢谢。

【问题讨论】:

  • 连接后执行mysql_query("SET NAMES 'utf8';");,结果为utf-8。
  • 你的数据库表的字符集是什么? utf8在mysql中支持有限的字符数,
  • 添加后 mysql_query("SET NAMES 'utf8';");结果: ”?????”改为:бѓ—бѓќбѓбѓњбѓбѓ™бѓ”

标签: php export georgian


【解决方案1】:
  1. 将表排序规则更改为 UTF8

  2. 在标题中添加:

    header('内容传输编码:二进制'); header("Content-Type: text/csv; charset=utf-8")

  3. 你也可以试试 mb_convert_encoding()。

您也可以查看此链接。How can I output a UTF-8 CSV in PHP that Excel will read properly?

【讨论】:

  • 我添加了 header('Content-Transfer-Encoding: binary');但同样的结果。 :(
  • 表格排序规则是 UTF8
  • 你添加了'content-type'吗?
  • 我添加了“mysql_query("SET NAMES 'utf8';");"在 db.php 和 header('Content-Transfer-Encoding: binary'); header("内容类型: text/csv; charset=utf-8");在 export.php 中
  • 如何在 PHP 中输​​出一个 UTF-8 CSV,Excel 可以正确读取?工作!谢谢!
猜你喜欢
  • 2011-03-01
  • 2018-01-18
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多