PHP export CSV when data having UTF8 charcters

后端 未结 4 1475
花落未央
花落未央 2021-02-14 03:49

In MySQL I have set my data field type to utf8_bin and i am storing data in Unicode. Texts are being properly displayed in web pages.

I want to generate ex

4条回答
  •  时光取名叫无心
    2021-02-14 04:28

    I had the same problem and I did it by combining two things:

    First, you have to change table collation to UTF8 e.g: "utf8_general_ci" (or just make sure of that), then add this code after your MySQL query:

    mysql_query("SET NAMES utf8");
    

    Like

    $result = mysql_query("SHOW COLUMNS FROM table WHERE Field NOT IN ('user_id', 'password')");
    mysql_query("SET NAMES utf8");
    

    And then, use this as header (adapt with your needs):

    header('Content-Description: File Transfer');
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header("Content-disposition: filename=".$filename.".csv");
    header('Content-Transfer-Encoding: binary');
    header('Pragma: public');
    print "\xEF\xBB\xBF"; // UTF-8 BOM
    print $csv_output;
    exit;
    

提交回复
热议问题