Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

write output query to a cvs file

 
   Database Help (Home) -> PHP RSS
Next:  Garbage characters from PHP to Javascript  
Author Message
Bre-x

External


Since: Jan 03, 2008
Posts: 4



(Msg. 1) Posted: Thu Jul 17, 2008 9:13 am
Post subject: write output query to a cvs file
Archived from groups: comp>lang>php (more info?)

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,

 >> Stay informed about: write output query to a cvs file 
Back to top
Login to vote
Bre-x

External


Since: Jan 03, 2008
Posts: 4



(Msg. 2) Posted: Thu Jul 17, 2008 10:31 am
Post subject: Re: write output query to a cvs file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

$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

 >> Stay informed about: write output query to a cvs file 
Back to top
Login to vote
Bre-x

External


Since: Jan 03, 2008
Posts: 4



(Msg. 3) Posted: Thu Jul 17, 2008 5:07 pm
Post subject: Re: write output query to a cvs file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jul 17, 1:21 pm, "Paul Lautman"
wrote:
> Bre-x wrote:
> >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.
 >> Stay informed about: write output query to a cvs file 
Back to top
Login to vote
Paul Lautman

External


Since: Oct 28, 2007
Posts: 128



(Msg. 4) Posted: Thu Jul 17, 2008 8:21 pm
Post subject: Re: write output query to a cvs file [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Bre-x wrote:
>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?
 >> Stay informed about: write output query to a cvs file 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Write a binary file - Can someone help me out, I can't figure out what I'm doing wrong to write to a file in binary mode. What's wrong with my code? <?php $fileName = "something.dat"; $string = "This is a string of text"; $ptr = fopen($fileName, 'w...

Write a file to disk with PHP - Hi all! Within my PHP page (on an Apache webserver), I must call a shell program that saves a file to disk. The program is executed correctly, but nothing is saved; I think this is a permission problem. I can't modify the program, so there is a way to..

Two simultaneous write accesses to a text file - If 2 people try to access the same text file at the same time to write to it - what happens in PHP ? What I mean is - presumably the first will be ok - But what will the second person actually see in his browser? And what will php do about it if not..

create file and write content in same code block - This a modified part of a larger script I made. I couldn't get it working so I made a smaller part of it - hoping to get it working. Well it still doesn't work :( The following code creates the new file, but does not write the $content strings to the..

Output XML file! - I have a XML variable (string). I want to output it to a XML file. I'm using <?php header("Content-type: application/gps"); header("Content-Disposition: attachment; filename=paring.xml"); print $xml; ?> But I get an error ...
   Database Help (Home) -> PHP All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]