我已阅读有关与函数stackoverflow
中重复值问题相关的问题的所有主题fputcsv
。不幸的是,我正在使用PDO
和psql
(所介绍的解决方案是使用简单的mysql
连接器描述的)
但首先我将描述我的问题,显示一些代码。这是我放的:
$db=ConnectionFactory::CreateConnection();
$fileName = 'somefile.csv';
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Description: File Transfer');
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$fileName}");
header("Expires: 0");
header("Pragma: public");
$fh = @fopen( 'php://output', 'w' );
$query = "SELECT * FROM person";
$results = $db->query($q)->fetchall();
$headerDisplayed = false;
foreach ( $results as $data ) {
// Add a header row if it hasn't been added yet
if ( !$headerDisplayed ) {
// Use the keys from $data as the titles
fputcsv($fh, array_keys($data));
$headerDisplayed = true;
}
// Put the data into the stream
fputcsv($fh, $data);
}
// Close the file
fclose($fh);
// Make sure nothing else is sent, our file is done
exit;
CSV
我得到的文件包含双值,例如:
"name","name","phone","phone","email","email"
"John","John","501231","501231","test","test"
"Bob","Bob","1111","1111","test2","test2"
当我尝试执行功能pg_fetch_array
时,我收到错误:
pg_fetch_array() expect parameter 1 to be resource, array given
我该怎么做才能修复我的 csv?