php - Error reading arabic in csv files with excel


I have this code that export Database table as csv and I have it like this ط¨ط§ط±ط§ط³ it should look like this علي. I do not know what is the it because of encoding or what. and my table Collation is utf8_general_ci

$mysqli = new mysqli('localhost', 'root', '', 'test');
 if ($mysqli->connect_errno)
$debug = $debug.'<br/>Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error;
$debug = $debug.'<br/>Connected to '. $mysqli->host_info;

function export($query_exp,$name)
require 'classs.php';

$filename = $name.' - '.date('Y.m.d').'.xls'; /*set desired output file name here*/

function cleanData(&$str)
                $str = preg_replace("/\t/", "\\t", $str);
                $str = preg_replace("/\r?\n/", "\\n", $str);
                if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
                if ($str=='') {$str='-';}

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/");

$flag = false;

if ($result = $mysqli->query($query_exp))
        if ($result->num_rows>0)
            while ($row = $result->fetch_assoc())
                    print implode("\t", array_keys($row)) . "\r\n";
                    $flag = true;
                array_walk($row, 'cleanData');
                print implode("\t", array_values($row)) . "\r\n";
        else { $debug = $debug.'<br/>Empty result'; /*DEBUG*/ }
else { $debug = $debug.'<br/>Oups, Query error!<br/>Query: '.$query_exp.'<br/>Error: '.$mysqli->error.'.'; /*DEBUG*/ }

require 'classs.php';
export('SELECT * FROM exel;','file');



After connecting, you need to tell mysql you are sending, and want to receive utf8:

  $mysql->query("set names 'utf-8'");

People are also looking for solutions to the problem: php - Code runs when refresh


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.