为什么我的CSV输出包含每列两次? [英] Why does my CSV output contain each column twice?

查看:110
本文介绍了为什么我的CSV输出包含每列两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下:

$name = 'registrations.csv';
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $name);
header('Pragma: no-cache');
header("Expires: 0");
$outstream = fopen("php://output", "w");
$results = mysql_query("select * from registrations");
while( $result = mysql_fetch_array($results) )
{
  fputcsv($outstream, $result);
}
fclose($outstream);

这很棒,除了它给出了每列两个。例如,我在表中有一个given_name列和一个surname列。生成的csv文件会显示每个列两次。为什么?

This works great, except it gives two of every column. For example, I have a given_name column and a surname column in the table. The resulting csv file shows each column twice. Why?

推荐答案

mysql_fetch_array()更改为 mysql_fetch_assoc() mysql_fetch_array()获取数据库结果的数字和关联数组。

Change mysql_fetch_array() to mysql_fetch_assoc(). mysql_fetch_array() fetches both a numeric and associative array of the database results.

while( $result = mysql_fetch_assoc$results) )
{
  fputcsv($outstream, $result);
}

这篇关于为什么我的CSV输出包含每列两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆