dongpo8250 2015-10-14 08:57
浏览 20
已采纳

数据库表到csv字符编码

So I found a script to get a csv file from the database table.

<?php 

header('Content-Type: text/csv; charset=utf-8 ');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('id','Name', 'Email', 'age','tekens'));

// fetch the data
mysql_connect('localhost', 'name', 'pass');
mysql_select_db('name');
$rows = mysql_query('SELECT * FROM info');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) 

fputcsv($output, $row);

?>

The code works but I have trouble with the characters. My database table looks like this : Database table

This is how I want it in my csv file. However my csv file looks like this : Csv file

In the csv file Хой shows like ???. Is there a way to fix this?

I am sorry if it's a duplicate. I tried so many different things.

  • 写回答

2条回答 默认 最新

  • dongzai2952 2015-10-14 10:11
    关注

    Okay guys, I fixed it with the help from CD001. The thing i changed was :

    // fetch the data
    mysql_connect('localhost', 'name', 'pass');
    mysql_select_db('name');
    $rows = mysql_query('SELECT * FROM info');
    

    to

    // fetch the data
    $link = mysql_connect('localhost', 'name', 'pass');
    mysql_set_charset('utf8',$link);
    mysql_select_db('name');
    $rows = mysql_query('SELECT * FROM info');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题