[SOLVED] fwrite() to write output of DB to file
Posted: Wed May 07, 2008 8:32 am
Hi all,
I hope you can help. I need to use the fwrite() to write the output of a select query to a file with a special delimiter separating the fields and pad certain fields.
I tried to do the above with the normal MySQL "INTO OUTFILE........" but because I have to pad certain fields and use a "รพ" as a delimiter things just went wrong because sed and awk aren't up to the challenge.
The reason for this situation is that I have a User DB of several stores and I'm in the process of streamlining some processes. Once every day the database has to be dump/extracted to a file which will be sent to all the stores and then the file has to be imported by the store software - which is written in COBOLT - thus the reason for the specifics.
The way that I have it is that first I do:
$query="select * from table";
$result=mysql_query($query) or die (mysql_error());
while ($row=mysql_fetch_array($result)) {
//specify all 78 rows
$var1 = $row['col1'];
$var2 = $row['col2']; //blah blah blah etc
}
// Write to file section
$file = "FILE";
$fo = fopen($file,'w');
$string = <--- What do I add here, do I add all 78 rows as "$var1,$var2...$var78\n"?
fwrite($fo, $string);
fclose($fo);
Where do I add the delimiter in all this, do I take the $var1 and sprintf() it first before I pass it to the fwrite() and is the query correct so far and with it add each row the file?
I've tried a couple of things and nothing seems to be working, can you please help me??
I hope you can help. I need to use the fwrite() to write the output of a select query to a file with a special delimiter separating the fields and pad certain fields.
I tried to do the above with the normal MySQL "INTO OUTFILE........" but because I have to pad certain fields and use a "รพ" as a delimiter things just went wrong because sed and awk aren't up to the challenge.
The reason for this situation is that I have a User DB of several stores and I'm in the process of streamlining some processes. Once every day the database has to be dump/extracted to a file which will be sent to all the stores and then the file has to be imported by the store software - which is written in COBOLT - thus the reason for the specifics.
The way that I have it is that first I do:
$query="select * from table";
$result=mysql_query($query) or die (mysql_error());
while ($row=mysql_fetch_array($result)) {
//specify all 78 rows
$var1 = $row['col1'];
$var2 = $row['col2']; //blah blah blah etc
}
// Write to file section
$file = "FILE";
$fo = fopen($file,'w');
$string = <--- What do I add here, do I add all 78 rows as "$var1,$var2...$var78\n"?
fwrite($fo, $string);
fclose($fo);
Where do I add the delimiter in all this, do I take the $var1 and sprintf() it first before I pass it to the fwrite() and is the query correct so far and with it add each row the file?
I've tried a couple of things and nothing seems to be working, can you please help me??