473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

write output query to a cvs file

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,
Jul 17 '08 #1
3 2699
$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
Jul 17 '08 #2
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?
Jul 17 '08 #3
On Jul 17, 1:21*pm, "Paul Lautman" <paul.laut...@btinternet.com>
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.
Jul 18 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joe Gazda | last post by:
I'm a relative newbie to PHP, but have been able to put together some PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
2
by: jeff_zhang446 | last post by:
Hi, I try to write some data structure into a file as below and would appreciate if someone can give me some advice here. The output file looks like below: row(1) 100 1 0 0 0 0 0 0 0 ...
1
by: joy_julia446 | last post by:
Hi there, I would like to print my result as below in an output file. I would appreciate if someone can give me some advice. RESULT ClassA,96.678,88.196,8.048,-0.233,456.231,5.890, 1...
14
by: dawnerd | last post by:
Hi, I am developing a CMS and came across something which has never happened to me before, and I re-wrote the specific script twice, both differently, and still had the same error. I'm not sure...
4
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
0
gauravgmbhr
by: gauravgmbhr | last post by:
Hi All, I am using a application that stores a scripts in a sql server table columns some times i require to use the scripts stored in table columns in a file. So i am lookin for a keyword...
4
by: myth0s | last post by:
(After thinking about it, maybe this should have been posted in the .NET forum...) Hi, I have a stored procedure in SQL Server that sends me a 3000+ char pre-formatted XML string. I use "FOR...
5
by: Jon Skeet [C# MVP] | last post by:
On Sep 9, 9:41 am, raylopez99 <raylope...@yahoo.comwrote: It's tricky in .NET for two reasons: 1) LINQ doesn't have any concept of "remove" 2) List<T>.RemoveAll doesn't pass in the index...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.