Connecting Tech Pros Worldwide Forums | Help | Site Map

write output query to a cvs file

Bre-x
Guest
 
Posts: n/a
#1: Jul 17 '08
I would like to output a odbc query to a text file. I have the
following php code but it isnt working.

<?

//conneccion

$conn=odbc_connect('DBA','','');
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql="SELECT BKAR_INV_SONUM FROM BKARINV WHERE BKAR_INV_ORDDTE >=
'2008-6-01' and BKAR_INV_ORDDTE <= '2008-6-30' AND BKAR_INV_LOC =
'UNI' AND BKAR_INV_INVDTE IS NULL";
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit("Error in SQL");}

while (odbc_fetch_array ($rs))
{
$so=odbc_result($rs,"BKAR_INV_SONUM");
$f = fopen("tmp_temp.csv", "w");
fwrite($f, $so);
}
fclose($f);
odbc_close($conn);
?>

I was reading on the net that I need to send the query output to an
array, but not a clue how to do it.

Thanks,

Bre-x
Guest
 
Posts: n/a
#2: Jul 17 '08

re: write output query to a cvs file


$sql = "SELECT bkar_inv_sonum
FROM BKARINV
WHERE bkar_inv_orddte BETWEEN('2008-06-01' and '2008-06-30')
AND bkar_inv_loc = 'UNI'
AND bkar_inv_invdte IS NULL";
if(!$rs = odbc_exec($conn, $sql)) {exit(odbc_errormsg());}
$f = fopen("tmp_temp.csv", "w");
while (odbc_fetch_array ($rs))
{
$so = odbc_result($rs,"bkar_inv_sonum")."\n";
fwrite($f, $so);
}
fclose($f);
odbc_close($conn);

Works very wll

Thank you

Bre-x
Paul Lautman
Guest
 
Posts: n/a
#3: Jul 17 '08

re: write output query to a cvs file


Bre-x wrote:
Quote:
>I would like to output a odbc query to a text file. I have the
following php code but it isnt working.
Some advice. "isnt working" [SIC] is as useful as a chocolate teapot! Things
can "not work" in many different ways.

What do you expect to see? What do you see?


Bre-x
Guest
 
Posts: n/a
#4: Jul 18 '08

re: write output query to a cvs file


On Jul 17, 1:21*pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
Quote:
Bre-x wrote:
Quote:
I would like to output a odbc query to a text file. I have the
following php code but it isnt working.
>
Some advice. "isnt working" [SIC] is as useful as a chocolate teapot! Things
can "not work" in many different ways.
>
What do you expect to see? What do you see?
from another group.....

Ah, okay. That is because you are creating the file inside of the
while, so you make a new file every time through. Try this...

Code: $sql = "SELECT bkar_inv_sonum
FROM BKARINV
WHERE bkar_inv_orddte BETWEEN('2008-06-01' and '2008-06-30')
AND bkar_inv_loc = 'UNI'
AND bkar_inv_invdte IS NULL";
if(!$rs = odbc_exec($conn, $sql)) {exit(odbc_errormsg());}
$f = fopen("tmp_temp.csv", "w");
while (odbc_fetch_array ($rs))
{
$so = odbc_result($rs,"bkar_inv_sonum")."\n";
fwrite($f, $so);
}
fclose($f);
odbc_close($conn);

Notice that the fopen happens before the while loop. It does not write
to the file until you use fclose, so it is building $f as an array.

For someone who understands, a few words will do.
Closed Thread