1

我已阅读有关与函数stackoverflow中重复值问题相关的问题的所有主题fputcsv。不幸的是,我正在使用PDOpsql(所介绍的解决方案是使用简单的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?

4

1 回答 1

7

PDO fetch() 或 fetchall() 的默认值为 PDO::ATTR_DEFAULT_FETCH_MODE,即以枚举和关联数组的形式检索行,因此您将获得重复

以枚举或关联方式获取,但不要简单地默认它

$db->query($q)->fetchall(PDO::FETCH_ASSOC);
于 2013-09-16T09:58:52.857 回答