Welcome, Guest. Please Login.
04/28/24 at 14:00:31
News:
Home Help Search Login


Pages: 1
Send Topic Print
Linux Gurus: write output query to cvs file (Read 3924 times)
Kelloggs
Active Member
*****


Do crazy people know
they are crazy?

Posts: 785
Gender: male
Linux Gurus: write output query to cvs file
07/17/08 at 09:10:15
 
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,  
 
Kelloggs
Back to top
 
 

Evo ERP - 35 Users
Dumped MS Access like a hot potato (VB.Net rules!!!)
Email WWW   IP Logged
GasGiant
Administrator
*****


How can I help?

Posts: 1523
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #1 - 07/17/08 at 09:17:10
 
What are you getting for errors?
Back to top
 
 


Email WWW GasGiant GasGiant 31012781 swordworlder swordworlder   IP Logged
Kelloggs
Active Member
*****


Do crazy people know
they are crazy?

Posts: 785
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #2 - 07/17/08 at 09:33:21
 
There is not errors. It output only one record (either the first or last)
 
 
Thnks
Back to top
 
 

Evo ERP - 35 Users
Dumped MS Access like a hot potato (VB.Net rules!!!)
Email WWW   IP Logged
GasGiant
Administrator
*****


How can I help?

Posts: 1523
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #3 - 07/17/08 at 09:46:31
 
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.
 
I don't know what you are trying to find, but using the null or zero date will also pick up canceled orders. To get open orders you'd want to use something like  
 
AND (bkar_invl_pqty <> '0' OR bkar_invl_ubo <> '0')
 
(instead of the NULL line) to grab open orders with quantities awaiting shipment.
 
I also fiddled with your SQL to use a BETWEEN instead of the other two cases. Much faster. Smiley
 
Back to top
 
 


Email WWW GasGiant GasGiant 31012781 swordworlder swordworlder   IP Logged
Kelloggs
Active Member
*****


Do crazy people know
they are crazy?

Posts: 785
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #4 - 07/17/08 at 10:30:30
 
The fwrite works very well.
 
 
I modified my sql and include "AND BKAR_INV_INVCD <> 'X"
 
Now, I can transfer data from PSQL to MySQL:
 
<?php
$connection = mysql_connect("localhost", "root", "") or die ("Unable to connect to server");
$db = mysql_select_db("intranet", $connection) or die ("Unable to select database");
$fcontents = file ('./tmp_temp.csv');
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode("\"", $line);
//$sql = "insert into uni_uninvoiceso (". implode("'", $arr).")";
$sql = "insert into uni_uninvoiceso values ('". $arr ."')";
mysql_query($sql);
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
mysql_close($connection);
?>
 
Thanks,
 
Kelloggs
Back to top
 
 

Evo ERP - 35 Users
Dumped MS Access like a hot potato (VB.Net rules!!!)
Email WWW   IP Logged
kkmfg
Senior Member
****


Ghost of the code

Posts: 411
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #5 - 07/17/08 at 10:54:52
 
Couldn't you have used ODBC to retrieve the data from PSQL and then directly save it in MySQL without the intervening CSV file step? I guess if the two parts are on different machines then maybe not... Otherwise why not do it directly?
Back to top
 
 

Collin
K & K Manufacturing, Inc

EvoERP Version 1-22-10 SP3
5 User Workgroup Pervasive 10
Email WWW   IP Logged
GasGiant
Administrator
*****


How can I help?

Posts: 1523
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #6 - 07/17/08 at 10:55:42
 
What is the reason for moving data from PSQL to MySQL? Building a reporting app or something for Internet use?
Back to top
 
 


Email WWW GasGiant GasGiant 31012781 swordworlder swordworlder   IP Logged
Kelloggs
Active Member
*****


Do crazy people know
they are crazy?

Posts: 785
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #7 - 07/17/08 at 11:39:01
 
We receive/ship SOs with an external app (ms access/myql)
 
I was ask to setup an automatic email (cron job) to be send to the sales dept with all Last month's uninvoice SOs that are fully receive and fully ship Tongue
 
Open SO = Pervasive
Rec/Ship  = Mysql
 
 Wink
 
Kelloggs
 
Back to top
 
 

Evo ERP - 35 Users
Dumped MS Access like a hot potato (VB.Net rules!!!)
Email WWW   IP Logged
GasGiant
Administrator
*****


How can I help?

Posts: 1523
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #8 - 07/17/08 at 11:53:33
 
Nifty. We invoice same day in most cases, but our production boss felt that he needed to see how things are progressing, so I gave him a report of "shipped, not invoiced" SOs that shows SO #, customer and the $ value of the released lines, both individually and as a total. Now it only takes him two seconds to find a reason to get on the invoicing clerk's case Cheesy
Back to top
 
 


Email WWW GasGiant GasGiant 31012781 swordworlder swordworlder   IP Logged
kkmfg
Senior Member
****


Ghost of the code

Posts: 411
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #9 - 07/17/08 at 12:16:35
 
Again, is there a reason why the same machine cannot see both the PSQL and MySQL DBs? Because, PHP can connect to both DBs at the same time. Depending on how your queries work you may be able to query against both one at a time. This might be less efficient processing wise but it could be less coding and screwing around. Heck, it might even end up faster anyway since you wouldn't need the dump/load cycle anymore.
 
Quote from Kelloggs on 07/17/08 at 11:39:01:
We receive/ship SOs with an external app (ms access/myql)

I was ask to setup an automatic email (cron job) to be send to the sales dept with all Last month's uninvoice SOs that are fully receive and fully ship Tongue

Open SO = Pervasive
Rec/Ship  = Mysql

Wink

Kelloggs


Back to top
 
 

Collin
K & K Manufacturing, Inc

EvoERP Version 1-22-10 SP3
5 User Workgroup Pervasive 10
Email WWW   IP Logged
Kelloggs
Active Member
*****


Do crazy people know
they are crazy?

Posts: 785
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #10 - 07/17/08 at 12:38:02
 
Mysql and PSQL are on diferent servers, diferent OS. Heck even diferent locations.
 
  Huh
 
Back to top
 
 

Evo ERP - 35 Users
Dumped MS Access like a hot potato (VB.Net rules!!!)
Email WWW   IP Logged
GasGiant
Administrator
*****


How can I help?

Posts: 1523
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #11 - 07/17/08 at 16:00:08
 
You can certainly read from one and write to the other in one PHP file. I do it in a several sections of my order entry code. All of our orders are entered in a web interface and the user has the option of saving the incomplete order as a Quote in a MySQL database on the web server or into the EDI tables. Saved quotes can be retrieved from MySQL and submitted to DBA/Evo in one, fluid motion.
Back to top
 
 


Email WWW GasGiant GasGiant 31012781 swordworlder swordworlder   IP Logged
kkmfg
Senior Member
****


Ghost of the code

Posts: 411
Gender: male
Re: Linux Gurus: write output query to cvs file
Reply #12 - 07/18/08 at 04:43:04
 
Quote from Kelloggs on 07/17/08 at 12:38:02:
Mysql and PSQL are on diferent servers, diferent OS. Heck even diferent locations.

 Huh


 
It doesn't matter if they are on different planets so long as the machine running the PHP script can SEE both of them. In other words, does it have a valid network route to both machines and are the firewalls setup such that it can make connections to both DB's on the two machines? If so then you can connect to both from the script.
Back to top
 
 

Collin
K & K Manufacturing, Inc

EvoERP Version 1-22-10 SP3
5 User Workgroup Pervasive 10
Email WWW   IP Logged
Pages: 1
Send Topic Print