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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.