2015-10-14 08:57
浏览 20


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


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');
$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');
    $rows = mysql_query('SELECT * FROM info');


    // fetch the data
    $link = mysql_connect('localhost', 'name', 'pass');
    $rows = mysql_query('SELECT * FROM info');
    打赏 评论
  • dqvtm82066 2015-10-14 09:17
    1. I do not think the issue with the first blank row of CSV (before even header is being set) is due to utf8 character
    2. some how a is being inserted in your csv as a first character even before you set header using fputcsv($output, array('id','Name', 'Email', 'age','tekens')); you can veryfy that by opening your csv in a text editor like notpad.
    3. You can just remove the while loop to avoid any confusion about utf8 characters and check from where does the is being written as the first character in data.csv.
    4. Having said that I was not able to replicate the same issue on my side with the same code and same kind of table
    打赏 评论

相关推荐 更多相似问题