PHP:将 MySQL 数据导出到 Excel 文件时出现问题
问题描述:
我正在将 MySQL 数据导出到一个 Excel 文件中,该文件包含印地语"数据,但出现了印地语निमà¥à¤¨.
I am exporting MySQL data into an Excel file which contains "Hindi" data but in place of Hindi निमà¥à¤¨ is appearing.
请帮助解决我的问题.我的 PHP 代码是:
Please help in resolving my issue. My PHP code is:
<?php
include 'connection.php';
$filename = "currentaffairquestions"; //your_file_name
$file_ending = "xls"; //file_extention
mysqli_set_charset($conn,'utf8');
header("Content-Type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
$sep = "\t";
$sql="SELECT question_number,question_name,choice1,choice2,choice3,choice4,answer from current_affairs_question_details where id='$_GET[currentaffairid]'";
$result=mysqli_query($conn,$sql);
while ($property = mysqli_fetch_field($result)) { //fetch table field name
echo $property->name."\t";
}
print("\n");
while($row = mysqli_fetch_row($result)) //fetch_table_data
{
$schema_insert = "";
for($j=0; $j< mysqli_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
?>
答
mysqli_set_charset($con,"utf8");连接后立即
mysqli_set_charset($con,"utf8"); immediately after connection