简体   繁体   中英

php csv show question marks

I'm trying to export a CSV file, which is working all good when export. But there are some problems when I open my csv file, there are some text change to "??????". But in phpmyadmin there is all fine. The text is encoding by utf-8.

Here is my code

<?php
$day = $_POST['day'];

$starttime = "'".$day.' 00:00:00'."'";
$stoptime = "'".$day.' 23:59:59'."'";

$uname = "*********";
$pass = "*********";
$host = "*********"; 
$database = "*********"; 

$connection=mysql_connect($host,$uname,$pass); 

echo mysql_error();
$selectdb=mysql_select_db($database) or die("Database could not be selected");  
$result=mysql_select_db($database)or die("database cannot be selected <br>");

$output = "";
$table = "***********";
$sql = mysql_query("SELECT * FROM $table WHERE applytime BETWEEN $starttime AND $stoptime");
$columns_total  = mysql_num_fields($sql);
$rowcount = mysql_num_rows($sql);

if($rowcount >= 1)
{
for ($i = 0; $i < $columns_total; $i++) {
    $heading =  mysql_field_name($sql, $i);
    $output = '"'.$heading.'",';
}
$output .="\n";

while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}

$filename =  "myFile.csv";

header("content-type:application/csv;charset=UTF-8");
header('Content-Disposition: attachment; filename='.$filename);

echo $output;
exit;
}

else
{
    echo("There is no record Today");
}

?>

Please help thanks.

I don't see a call to the mysql_set_charset function.

mysql_set_charset('utf8');

Was that omission an oversight? Or was that purposefully omitted because its already been confirmed that the client characterset is set appropriately without that?


Why are we writing code that uses the deprecated mysql_ functions, when both mysqli and PDO interfaces are available? And why are we writing code that is vulnerable to SQL Injection, by incorporating potentially unsafe values into SQL text?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM