title: MySQL to Excel charset issue
body: <p>I have a database set up which accepts user registrations and their details etc. I'm looking to export the database to an excel file using php.</p>
  
  <p>The problem I am having is that some of the entrants have entered foreign characters in, such as Turkish, which has been written into the database 'incorrectly' - as far as I have ascertained, the charset was likely set up incorrectly when it was first made.</p>
  
  <p>I have made my code to export the database into excel (below) but I cannot get the Excel document to show correctly regardless of how I try to encode the data</p>
  
  <pre><code>&lt;?php 
  
  require_once('../php/db.php');
  
  header("Content-type: application/octet-stream");
  header("Content-Disposition: attachment; filename=Download.xls");
  header("Pragma: no-cache");
  header("Expires: 0");
  
  $query =    "SELECT * FROM users";
  
  $result = mysqli_query($link, $query);
  if($result) {
      $count = mysqli_num_rows($result);
      for($i=0; $i&lt;$count; $i++) {
          $field = mysqli_fetch_field($result);
          $header .= $field-&gt;name."\t";
          while($row = mysqli_fetch_row($result)) {
              $line = '';
              foreach($row as $value) {
                  if((!isset($value)) OR ($value == "")) {
                      $value = "\t";
                  } else {
                      $value = str_replace('"', '""', $value);
                      $value = '"'.$value.'"'."\t";
                  }
                  $line .= $value;
              }
              $data .= trim($line)."\n";
          }
          $data = str_replace("\r", "", $data);
          if($data == "") {
              $data = "\n(0) Records Found!\n";
          }
      }
      print mb_convert_encoding("$header\n$data", 'UTF-16LE', 'UTF-8');
  } else die(mysqli_error());
  ?&gt;
  </code></pre>
  
  <p>When I do this it comes up with an error when opening it up saying that Excel doesn't recognise the file type, it opens the document but its drawn boxes around all the Turkish characters its tried to write.</p>
  
  <p>I'm no PHP expert this is just information I've kind of pieced together.</p>
  
  <p>Can anyone give me a hand?</p>
  
  <p>Much appreciated</p>
  
  <p>Moz</p>