如何避免fputcsv中不必要的双引号(“)?

时间:2018-04-30 10:51:31

标签: php oracle fwrite fputcsv

我将使用fputcsv函数通过PHP导出文本文件(而不是CSV文件)中的查询输出,但是当我打开文本文件时,会添加额外的双引号(“)开始和结束,也在中间:

"SET: NEID=35;"
"ADD OPDNAL:ENTR=571,DIGIT=""703901943"",NANNAT=ALL,NAME=""CallingFilter"",RATIO2=0;"

这是我想要的结果:

SET: NEID=35;
ADD OPDNAL:ENTR=571,DIGIT="703901943",NANNAT=ALL,NAME="Calling Filter",RATIO2=0; 

我的代码如下:

$sql="select 'SET: NEID=35;' as ACC_OFFNET from dual
          union all
          SELECT ACC_OFFNET FROM JLD_ADD WHERE ACC_OFFNET NOT IN (SELECT ACC_OFFNET FROM JLD_ADD_BK)
          union all
          select 'syn' as ACC_OFFNET from dual";

$result = odbc_exec($connect_raidprd, $sql) or die("Couldn't execute query! ".odbc_errormsg());

$date=date('Y-m-d H:ia',time());
$update = str_replace(':','-',$date);

$filenameJLD = "JLD-ADD-". $update.".csv";

$handle = fopen($filenameJLD, 'w+');

while ($row =odbc_fetch_array($result)) {
    fputcsv($handle,array($row['ACC_OFFNET']));     
}

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

或者,fwrite是否可以接受数组作为参数?

1 个答案:

答案 0 :(得分:0)

我认为这就是你要找的东西。这将为您提供干净的Oracle SQL输出。首先在Oracle SQL中进行测试,只需复制,粘贴和运行即可。然后将其添加到您的PHP并将其包含在"如果需要。我不会使用PHP,不知道它是如何在那里工作的:

SELECT 'select ''SET: NEID=35;'' as ACC_OFFNET from dual
    union all
    SELECT ACC_OFFNET FROM JLD_ADD WHERE ACC_OFFNET NOT IN (SELECT ACC_OFFNET FROM JLD_ADD_BK)
    union all
    select ''syn'' as ACC_OFFNET from dual;'
 FROM dual
/

输出 - 此查询将在Oracle Sql中运行,不会添加任何双引号:

    select 'SET: NEID=35;' as ACC_OFFNET from dual
    union all
    SELECT ACC_OFFNET FROM JLD_ADD WHERE ACC_OFFNET NOT IN (SELECT ACC_OFFNET FROM JLD_ADD_BK)
    union all
    select 'syn' as ACC_OFFNET from dual;