I am currently able to export my MySQL query to a tab delimited text file, however it is saving with a UTF8 encoding. A requirement of this project is that it saves as a Unicode-16LE text file.
My current function looks something like this:
$select = "SELECT -removed-";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name( $export , $i ) . "\t";
}
while ($row = mysql_fetch_row($export)) {
$line = '';
foreach ($row as $value) {
if ((!isset($value)) || ($value == "")) {
$value = "\t";
} else {
$value = str_replace( '"' , '""' , $value );
$value = '' . $value . '' . "\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\n";
}
$data = str_replace( "\r" , "" , $data );
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=my_file.txt");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";
Any advice would be appreciated!
Thanks in advance.
HTTP headers are fairly irrelevant when you are creating a file from database info. Since your app is currently using UTF-8, you have two choices:
Fetch the information directly in UTF-16, i.e., run
SET NAMES somethingelseinstead ofSET NAMES utf8, where somethingelse is your target encoding. You can fetch available encodings withSHOW CHARSET.Fetch the information in UTF-8 and convert it to Unicode-16LE before saving it to file. You can use iconv() or mb_convert_encoding().