Click here to get back home

write output query to a cvs file

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
write output query to a cvs file Bre-x 07-17-2008
Posted by Bre-x on July 17, 2008, 12:13 pm
Please log in for more thread options
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,

Posted by Bre-x on July 17, 2008, 1:31 pm
Please log in for more thread options
$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))
        $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

Posted by Paul Lautman on July 17, 2008, 3:21 pm
Please log in for more thread options
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?



Posted by Bre-x on July 17, 2008, 8:07 pm
Please log in for more thread options
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! Thi=
ngs
> 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 =3D "SELECT bkar_inv_sonum
                FROM BKARINV
                WHERE bkar_inv_orddte BETWEEN('2008-06-01' and '2008-06-30')
                        AND bkar_inv_loc =3D 'UNI'
                        AND bkar_inv_invdte IS NULL";
        if(!$rs =3D odbc_exec($conn, $sql))
        $f =3D fopen("tmp_temp.csv", "w");
        while (odbc_fetch_array ($rs))
        {
         $so =3D 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.

Similar ThreadsPosted
How to write PHP standard output on a text file? February 10, 2006, 12:51 pm
Write custom column names to query result file May 3, 2005, 12:42 pm
How would I write a regexp on a MySQL query February 28, 2006, 11:26 pm
PHP/IIS: File Read/Write OK, File Unlink Denied March 22, 2006, 5:10 am
Display sql query output in html table June 23, 2006, 2:44 pm
How to use for loop to write to file? July 20, 2004, 8:03 am
Write to text file May 26, 2005, 10:01 pm
Write a file to disk with PHP September 14, 2007, 4:47 am
Write to binary file December 8, 2007, 3:47 pm
Getting a file via http or evaluating a php file and getting its output October 25, 2004, 1:10 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap