jigen7 Posted October 3, 2007 Share Posted October 3, 2007 can any one help me make a function that take in a query then output the results in CSV format ?? i have found here a sample but its not working for me <? $db = new MyDb; $db->db_connect($host, $dbuser, $password); # connect to database $db->db_select($database); $csv_output = '"column 1","column2"'; $csv_output .= "\015\012"; $result = mysql_query("select * from users"); while($row = mysql_fetch_array($result)) { $csv_output .= '"'.$row[col1].'","'.$row[col2].'"'; $csv_output .= "\015\012"; } //You cannot have the breaks in the same feed as the content. header("Content-type: application/vnd.ms-excel"); header("Content-disposition: csv; filename=document_" . date("Ymd") . ".csv"); print $csv_output; exit; ?> Quote Link to comment Share on other sites More sharing options...
jaymc Posted October 3, 2007 Share Posted October 3, 2007 Its really not hard Each row is seperated by a line (\n) Each field is seperated by a comma That in a loop is half the script done $csv_output .= '"'.$row[col1].'","'.$row[col2].'"' Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.