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 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面