-1

I am going to export an output of a query in a text file (not CSV file) via PHP using fputcsv function, but when I open the text file, extra double-quotes (") are added at the start & end, and also in the middle:

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

This is the result that I want:

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

My code is as below:

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

Alternatively, is it possible for fwrite to accept array as the argument?

Andrew T.
  • 4,592
  • 7
  • 38
  • 55
Meena
  • 55
  • 7
  • `fwrite` does not accept array, but you can `implode()` the array. Alternatively you can use `file_put_contents()` too – frz3993 May 01 '18 at 04:54
  • Thank You so much , it worked but but only cannot do line break after each row even i use
    as this. while ($row =odbc_fetch_array($result)) { $data=implode("
    ",array($row['ACC_OFFNET'])); fwrite($handle,$data); }
    – Meena May 01 '18 at 07:39
  • thanks, the problem has been solved. – Meena May 02 '18 at 10:12

1 Answers1

0

I think this is what you are looking for. This will give you clean Oracle SQL output. Test this in Oracle SQL first, simply copy, paste and run. Then add this to your PHP and enclose this in " if required. I'm not into PHP, not sure how it works there:

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
/

Output - this query will run in Oracle Sql and will not add any double quotes:

    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;
Art
  • 5,106
  • 1
  • 18
  • 20
  • No , my question is for those who are expert in PHP , how to use fwrite() function instead of fputcsv() function to accept array. i need out out in sample text format – Meena May 01 '18 at 04:19
  • That's ok. But if your Oracle SQL format is not correct then query will fail in Oracle. So, i would start with Oracle first. Your double quotation seems coming from your second select. You need to post more info about your data and create some test data similar to your data from second query... – Art May 01 '18 at 15:33