尝试将以下工作代码转换为PDO时,遇到的问题是fputcsv($output,array)中设置的自定义标题。我真的想离开所有的msql_u函数,所以我会非常感谢一些帮助。

<?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);
}
?>

最佳答案

这是我想出来的,看起来和原来的一样。如果您有任何问题,请告诉我,它按预期工作,我摆脱了mysql\u函数。

<?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);

?>

07-24 17:19
查看更多