<?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编写的数据都会输出:&#34; ??????&#34;
有人可以帮我修改这个脚本吗?
谢谢。
答案 0 :(得分:0)
将表格排序更改为UTF8
在标题中添加:
标题(&#39; Content-Transfer-Encoding:binary&#39;); 标题(&#34;内容类型:text / csv; charset = utf-8&#34;)
您也可以尝试使用mb_convert_encoding()。
您也可以查看此链接。How can I output a UTF-8 CSV in PHP that Excel will read properly?