2015-01-21 81 views
0

我的表中有很多字段已经序列化,以保持字段数量不变。对于一切,因为它存储数据并在需要时我可以用下面的一个例子可以完美运行:使用fputcsv将数组存入CSV CSV文件

$dateofbirth = unserialize($row['dateofbirth']); 
$dobday = $dateofbirth[0]; 
$dobmonth = $dateofbirth[1]; 
$dobyear = $dateofbirth[2]; 

出生日期存储为dd,mm,yyyy和一切我可以把它的罚款。我的问题是,现在我试图使用fputcsv使用下面创建一个CSV文件:

$result = mysqli_query($con, 'SELECT u.user_id, b.dateofbirth FROM Users u INNER JOIN Basic b USING (user_id) ORDER BY user_id DESC'); 

$fp = fopen('latest.csv', 'w'); 

fputcsv($fp, array('User ID', 'DOB')); 

的CSV产生,但出生列在其输出为"*a:3:{i:0;s:2:"03";i:1;s:2:"02";i:2;s:4:"1986";}*"的CSV的日期,因为它仍明显序列化。我处理这些领域的最佳和最简单的方法是什么?

非常感谢提前。

+0

已经回答了在 http://stackoverflow.com/questions/28019566/csv-php-mysql-data-export-all-data-is-being-exported-in-one-column/28019579# 28019579 – 2015-01-21 11:01:19

+0

@SunilPachlangia根据我对这个问题的理解,他们在1列中拥有所有的值。我有多个列,其中一些是数组,其中一些不是。 – BN83 2015-01-21 11:04:35

回答

0

U可以试试这个:

header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-type: text/csv"); 
header("Content-Disposition: attachment; filename=latest.csv");// Disable caching 
header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1 
header("Pragma: no-cache"); // HTTP 1.0 
header("Expires: 0"); // Proxies 

$fp = fopen('latest.csv', 'w'); 

while($row = mysqli_fetch_array($result)){ 

fputcsv($fp,array($row[0],$row[1])); 
} 

//或者你可以在没有头使用上述

或设置一些格式,出生日期,如:

$rowdob = date("M d, Y - g:i:s A", strtotime($row[0])); 

的同时循环并将$ rowdob传递给fputcsv();