I'm going to export data in SQL to Excel (csv) using PHP. I can export all data from SQL to Excel, but it is unknown Japanese language. For now, i can solved it; but other problem is about each row in SQL will export into one column in Excel.
Example (data in SQL)
ccode country
US United State
UK United Kingdom
FR France
KO Korea
JP 東京
After export to Excel (csv)
A
1 ccode,country
2 US,United State
3 UK,United Kingdom
4 FR,France
5 KO,Korea
6 JP,東京
Here is my code
<?php
header("Content-type: text/csv; charset=UTF-8");
header('Content-Disposition: attachment; filename=Export.csv');
//connection
$con = mysql_connect('localhost', 'root', '');
if(!$con){
echo "Error connection";
}
//select db
$select_db = mysql_select_db('country', $con);
if(!$select_db){
echo "Error to select database";
}
mysql_set_charset("utf8", $con);
//Mysql query to get records from datanbase
$user_query = mysql_query('SELECT * FROM countries');
//While loop to fetch the records
$contents = "ccode,country\n";
while($row = mysql_fetch_array($user_query))
{
$contents.=$row['ccode'].",";
$contents.=$row['country']."\n";
}
$contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
print $contents_final;
?>
Here is what i want after export to Excel (csv)
A B
1 ccode country
2 US United State
3 UK United Kingdom
4 FR France
5 KO Korea
6 JP 東京
Can anyone help me to solve this problem? I'm appreciate to your help!
Thank in advance.