Aneesh Asokan
Aneesh Asokan

Reputation: 31

PHP MySQL csv export

I am trying to export MySQL data to csv file. Here is my code

header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");           
header("Content-Type: application/octet-stream");           
header("Content-Disposition: attachment;filename=data.csv");
header("Content-Transfer-Encoding: binary");   
$output = fopen('php://output', 'w');
fputcsv($output, array('Id', 'Data 1', 'Data 2','Data 3'));

foreach ($data as $row)
{                    
     fputcsv($output, $row, ',', '"'); //arabic data
}
fclose($output);           
return chr(255) . chr(254) . mb_convert_encoding(ob_get_clean(), 'UTF-16LE', 'UTF-8');

The data contain Arabic and Hindi languages. The code results with all data with comma separated in one cell with new line.

I want to download this file with data in each cell. I tried this in last of code

return ob_get_clean();

it shows data in each row, but data expect English not loaded correctly.

How can solve this?

Upvotes: 1

Views: 137

Answers (1)

Paresh Barad
Paresh Barad

Reputation: 1609

The mysqli_set_charset() function specifies the default character set to be used when sending data from and to the database server.

Please use before Mysql query.

mysqli_set_charset( $db, 'utf8');

Upvotes: 1

Related Questions