OCIFetch error?


#1

Hi all,
looking for some assistance!
As I am new to this I do not have much experience with OCI data.

I need to output some oracle sql data to a csv file however I get the error:
ocifetch(): supplied resource is not a valid oci8 statement resource

<?php 
//BLUEONE CONN
$conn = oci_connect('XXXX', 'XXXX', 'XXXXX');
if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
//POST VARIABLES
$fromdate = $_POST['fromdate'];
$todate = $_POST['todate'];

//VARIABLES TIME FORMAT
$fromdate2 = date("dMY", strtotime($fromdate));
$todate2 = date("dMY", strtotime($todate));



$contents="Dof,Flightnr,DEP,ARR,STD,ATD,DLTIME,DLCODE,REMARK\n";
$query = ("SELECT
     trunc(master.v_flights.flt_com_dep_blk) AS dof,
     master.v_flights.lfc_id
     || master.v_flights.flt_number AS flightnr,
     master.v_flights.flt_apt_iata_code_dep   AS dep,
     master.v_flights.flt_apt_iata_code_arr   AS arr,
     TO_CHAR(master.v_flights.flt_com_dep_blk,'HH24:MI') AS std,
     TO_CHAR(master.v_flights.flt_mvt_db,'HH24:MI') AS atd,
     TO_CHAR(master.v_delay_codes.dec_time,'HH24:MI') AS dltime,
     master.v_delay_codes.dec_ldc_id          AS dlcode,
     master.v_flights.flt_rmks                AS remarks
 FROM
     master.v_flights,
     master.v_delay_codes
 WHERE
     master.v_flights.flt_id = master.v_delay_codes.dec_flt_id (+)
     AND master.v_delay_codes.dec_ldc_id BETWEEN '41' AND '49'
     AND master.v_flights.lfs_label = 'Operated'
 ORDER BY
     dof,
     std
		 ");


    $stmt = ociparse($conn, $query);

OCIDefineByName($stmt, "DOF", $dof);
OCIDefineByName($stmt, "flightnr", $flightnr);
OCIDefineByName($stmt, "dep", $dep);
OCIDefineByName($stmt, "arr", $arr);
OCIDefineByName($stmt, "std", $std); 
OCIDefineByName($stmt, "atd", $atd); 
OCIDefineByName($stmt, "dltime", $dltime); 
OCIDefineByName($stmt, "dlcode", $dlcode); 
OCIDefineByName($stmt, "remarks", $remarks); 


OCIExecute($stmt);

 $handle = fopen("myfile.csv", "w");
fwrite($handle, "Dof,Flightnr,DEP,ARR,STD,ATD,DLTIME,DLCODE,REMARK\n");
while (ocifetch($stmt)) {
   $contents.=$dof.",";
   $contents.=$flightnr.",";
   $contents.=$dep.",";
   $contents.=$arr.",";
   $contents.=$std.",";
   $contents.=$atd.",";
   $contents.=$dltime.",";
   $contents.=$dlcode.",";
   $contents.=$remarks."\n";
fwrite($handle, $contents);

fclose($handle);

OCIFreeStatement($stmt);
    }

?> 

Any suggestions?

Thanks for the help!
Yannick


#2

I have experience with Oracle, just not with PHP as the backend language, so, this is a guess.
https://community.oracle.com/thread/1053354