PHP fetching SQL - double records in row

3489 views php
1

Im fetching data from SQL and write in csv. But in file I see double record in a row. What happend?

"2018-11-05 15:50:41","2018-11-05 15:50:41",1078,1078

 <?php  

    $connect = oci_connect("login","pass","db");  
    $output = fopen("graph.csv", "a");  
    //fputcsv($output, array('TIME','Orders'));  
    $query = "SELECT TO_CHAR(SYSDATE, 'YYYY-mm-dd HH24:MI:ss'), count(items) FROM box_table";  
    $result = oci_parse($connect, $query);
    $r=oci_execute($result);      
    while ($row = oci_fetch_array($result, OCI_BOTH)) {
        fputcsv($output, $row);  
    }  
    fclose($output);  

 ?>  

answered question

Knowing the purpose of parameters for given function is always a good start. php.net/manual/en/function.oci-fetch-array.php

oci_fetch_array($result, OCI_ASSOC)

1 Answer

2

The problem is that in the fetch you fetch the data using OCI_BOTH which fetches the data as an associative array and as a numeric indexed set of values.

OCI_BOTH Returns an array with both associative and numeric indices. This is the same as OCI_ASSOC + OCI_NUM and is the default behavior.

while ($row = oci_fetch_array($result, OCI_BOTH))  

use OCI_ASSOC or OCI_NUM...

while ($row = oci_fetch_array($result, OCI_ASSOC))  

posted this

Have an answer?

JD

Please login first before posting an answer.