PDA

View Full Version : Trying to output a comma delimited file (csv)



Bobafart
09-18-2009, 09:47 PM
Hello, I am trying to create a script that output a csv file from data I am querying from a mySQL table. Keep getting this error:

Fatal error: Call to undefined function phpheader() in /home/foo/bar/exportmycsv.php on line 1



<?php
header('Content-type: application/x-msdownload');
header('Content-Disposition: attachment; filename=exportcsv.csv');
header('Pragma: no-cache');
header('Expires: 0');
include("myconnect.php");
$sql = "SELECT theDate,theTitle,theContent FROM mycal ORDER BY theDate ASC";
$result = mysql_query($sql);
if($result){
echo "Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private\r\n"; //header
while($row = mysql_fetch_array($result)){
echo "\"$row[theTitle]\",\"$row[theDate]\",\"false\",\"$row[theDate]\",\"false\",\"false\",\"$row[theContent]\",\",\"false\"\r\n"; //data
}
}
?>



can someone tell me what I am doing incorrect? it all appears satisfactory to my eyes

Fumigator
09-18-2009, 10:17 PM
I don't see any call to phpheader()... is that script called exportmycsv.php? Are you looking at the right folder?

MattF
09-18-2009, 11:36 PM
Add a blank line after the opening <?php tag and see if that makes any difference. What format are you saving the file in? Rich text, by any chance?

karthikm19
09-18-2009, 11:47 PM
check this line
---------------

echo "\"$row[theTitle]\",\"$row[theDate]\",\"false\",\"$row[theDate]\",\"false\",\"false\",\"$row[theContent]\",\",\"false\"\r\n"; //data

replace this line with the following line and try

echo "\"" . $row[theTitle] . "\",\"" . $row[theDate] . "\",\"false\",\"" . $row[theDate] . "\",\"false\",\"false\",\"" . $row[theContent] . "\",\",\"false\"\r\n"; //data

orca8767
09-19-2009, 01:48 AM
It'd be much better to do:

echo '"'. $row['theTitle'] .'","'. $row['theDate'] .'","false","'. $row['theDate'] .'","false","false","'. $row['theContent'] .'",","false"'. "\r\n"; //data

Bobafart
09-19-2009, 06:48 PM
It'd be much better to do:

echo '"'. $row['theTitle'] .'","'. $row['theDate'] .'","false","'. $row['theDate'] .'","false","false","'. $row['theContent'] .'",","false"'. "\r\n"; //data

Thanks for all of your help gents

Orca, you rock. The problem was solved with your fabulous post.

Thanks again all