swr,
I am using the code below, taken generally from what you offered in that other thread. I added the db connect, my sql query and I removed the " from the output file. It all works great.
Now I what I want to do is create a line that prints the column headers first. Now these are not the column headers in the db. I want the first line of the csv file to be: hourly,hour,temp,rh,precip,ws,wd
Secondly, at what step in this code do I change the data in the fields such as making the date dd/mm/yyyy from yyyy-mm-dd or the time from hh:mm:ss to hh? I can do these manipulations, I just don't know where to put the code for it.
<?php
if (!$link = mysql_connect('localhost', 'uuuu', 'ppppp'))
exit("Unable to make a connection to the database");
if (!mysql_select_db("ops_db", $link))
exit("Unable to select the database");
$delimiter = ','; // delimiter is sometimes ; or | instead
$quote = ''; // quote is sometimes ' instead
$your_query="SELECT Date_Valid, Time_Valid, Temp, RH, Rn_1, WSpd, DDir FROM Actual_Hourly_2004 WHERE `FMFP_ID` = '".$station."' ORDER by 'Date_Valid', 'Time_Valid' DESC";
$result = mysql_query($your_query) or die(mysql_error());
header("Content-type: text/csv");
while ($record = mysql_fetch_row($result)) {
$first = true;
foreach ($record as $field) {
if (!$first) echo $delimiter;
$field = str_replace($quote, $quote.$quote, $field); // escape quotes
echo $quote.$field.$quote;
$first = false;
}
echo "\n";
}
?>