1

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?

1 Answer 1

1

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');
Sign up to request clarification or add additional context in comments.

1 Comment

header('Content-Type: text/csv; charset=utf-8' ); added in code and not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.