fputcsv与自定义标题

3

我想将以下可工作的代码转换为PDO,但我遇到了在 fputcsv($output,array) 中设定自定义标题的问题。我真的想远离所有mysql_函数,所以非常希望得到一些帮助。

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

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


//MySQL Connect
removed

// output the column headings
fputcsv($output, array('Title','InclusiveDates','CollectionID','LocationID','Content','RangeValue','Section','Shelf','Extent','ExtentUnitID'));

// fetch the data

$rows = mysql_query('SELECT 
coll.title AS CollectionTitle, 
coll.inclusivedates AS InclusiveDates,
coll.ID AS CollectionID, 
collloc.LocationID,
collloc.Content,
collloc.RangeValue,
collloc.Section,
collloc.Shelf,
collloc.Extent,
collloc.ExtentUnitID
FROM tblCollections_Collections coll 
JOIN tblCollections_CollectionLocationIndex collloc 
ON collloc.CollectionID = coll.id 
ORDER BY coll.ID');

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

?>

我已经做到了这一步,但仍无法使其正常工作。问题在于我无法设置CSV文件中的自定义列名,并且它也不能导出为CSV格式。

<?php
$conn = removed


$query = "SELECT 
coll.title AS CollectionTitle, 
coll.inclusivedates AS InclusiveDates,
coll.ID AS CollectionID, 
collloc.LocationID,
collloc.Content,
collloc.RangeValue,
collloc.Section,
collloc.Shelf,
collloc.Extent,
collloc.ExtentUnitID
FROM tblCollections_Collections coll 
JOIN tblCollections_CollectionLocationIndex collloc 
ON collloc.CollectionID = coll.id 
ORDER BY coll.ID";
$stmt = $conn->prepare($query);

// Execute the statement
$stmt->execute();

var_dump($stmt->fetch(PDO::FETCH_ASSOC));

$data = fopen('file.csv', 'w');

$header = array();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
if(empty($header)){ // do it only once!
  $header = array_keys($row); // get the columnnames
  fputcsv($data, $header); // put them in csv
}

echo "Success";
// Export every row to a file
fputcsv($data, $row);
}
?>

1
问题与PDO / mysql有何关系?在这两个脚本中,您处理csv文件的方式基本上是不同的。 - jeroen
不,我只是无法使用PDO而不是mysql_产生相同的结果。 :-( - Carlo
1
你怎么知道的?你已经改变了这么多,输出到不同的媒介上,很难确定问题出在哪里。你应该先仅仅切换到PDO,等你把它搞定后再改变其他逻辑。 - jeroen
请纠正我如果我错了,你不是要使用 fclose 函数关闭文件吗? - Matt
PDO的输出与mysql_相同,即关联数组。因此,从一个转换到另一个应该很简单。PS:如果您没有使用绑定参数,可以使用query()代替prepare() - david strachan
1个回答

5

我想到的方案如下,它似乎和原来的一样有效。如果有任何疑虑,请告诉我,这个方案按预期工作,而且我已经摆脱了mysql_函数。

<?php

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=collections.csv');

try {
$conn = removed
  }
catch(PDOException $e)
{
echo $e->getMessage();
}

$sql = "SELECT 
coll.title AS CollectionTitle, 
coll.inclusivedates AS InclusiveDates,
coll.ID AS CollectionID, 
collloc.LocationID,
collloc.Content,
collloc.RangeValue,
collloc.Section,
collloc.Shelf,
collloc.Extent,
collloc.ExtentUnitID
FROM tblCollections_Collections coll 
JOIN tblCollections_CollectionLocationIndex collloc 
ON collloc.CollectionID = coll.id 
ORDER BY coll.ID";

$results = $conn->query($sql);

// Pick a filename and destination directory for the file
// Remember that the folder where you want to write the file has to be writable
$filename = "collections.csv";

// Actually create the file
// The w+ parameter will wipe out and overwrite any existing file with the same name
$handle = fopen('php://output', 'w');

// Write the spreadsheet column titles / labels
fputcsv($handle,   array('Title','InclusiveDates','CollectionID','LocationID','Content','RangeValue','Section','Shelf','Extent','ExtentUnitID'));

// Write all the user records to the spreadsheet
foreach($results as $row)
{
fputcsv($handle, array($row['CollectionTitle'], $row['InclusiveDates'], $row['CollectionID'], $row['LocationID'], $row['Content'], $row['RangeValue'], $row['Section'], $row['Shelf'], $row['Extent'], $row['ExtentUnitID']));
}

// Finish writing the file
fclose($handle);

?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接