Jump to content

Downloadable CSV from Oracle DB tables


alexmahone

Recommended Posts

The below code is not stamping the table rows to CSV, I get empty CSV download file.. pls help 

 

<?php

  $conn = oci_connect('uid', 'pass', 

 

'(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=hostname)(PORT=1521))(CONNECT_DATA=(SID=database)))');

if (!$conn) {

    $e = oci_error();

  echo("<br/>".$e);

    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);

}

 

$stid = oci_parse($conn, 'SELECT server,ipaddress FROM table where rownum<=50 order by kill_time desc  ');

if (!$stid) {

    $e = oci_error($conn);

echo($e);

    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);

}

 

// Perform the logic of the query

$r = oci_execute($stid);

if (!$r) {

    $e = oci_error($stid);

    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);

}

 

// Fetch the results of the query

 

 

 

 

$ncols = oci_num_fields($stid);

echo "<tr>\n";

for ($i = 1; $i <= $ncols; ++$i) {

    $colname = oci_field_name($stid, $i);

   }

 

 

// Query Database

$filename = 'file.csv';

 

$out = '';

 

// fiels to export

$out .='server, ipadress';

$out .="\n";

 

// Add all values in the table

while ($l = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {

for ($i = 0; $i < 2; $i++) {

$out .=''.$l["$i"].',';

}

$out .="\n";

}

// Output to browser with appropriate mime type

header("Content-type: text/x-csv");

header("Content-Disposition: attachment; filename=$filename");

echo $out;

exit;

  ?>

Link to comment
https://forums.phpfreaks.com/topic/284352-downloadable-csv-from-oracle-db-tables/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.