php中的csv,逗号没有反转的逗号值

时间:2017-12-20 03:33:15

标签: php csv comma

我搜索了论坛,找不到类似的情况。 我有以下代码来创建CSV文件:

$query2 = "SELECT * FROM mydb where amount='50'";
$result2 = mysql_query($query2);
while($row2 = mysql_fetch_array($result2))
  {
   $email = $row2['email'];
   $amount = $row2['amount'];
   $name = $row2['name'];

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// create 2 decimal value on the amount
$mem50 = number_format(floor($amount*100)/100,2, '.', '');

// output the column headings
fputcsv($output, array("$email", "$mem50", "$name", "\r\n"));

}

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($row2)) fputcsv($output, $row);

问题是我的以下结果:

mytestemail1@gmail.com,50.00,"test name1","
"mytestemail2@gmail.com,50.00,"test name2","
"mytestemail3@gmail.com,50.00,"test name3","

我要找的结果应该是:

mytestemail1@gmail.com,50.00,test name1
mytestemail2@gmail.com,50.00,test name2
mytestemail3@gmail.com,50.00,test name3

由于一些奇怪的原因,它最后会添加",。 如果我使用正常值,那么脚本可以工作 - 比如aaa,bbb,ccc

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

fputcsv($output, array("$email", "$mem50", "$name", "\r\n"));

应该是:

fputcsv($output, array($email, $mem50, $name));