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; ?> Link to comment https://forums.phpfreaks.com/topic/71615-helpexport-sql-data-into-csv-format/ 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].'"' Link to comment https://forums.phpfreaks.com/topic/71615-helpexport-sql-data-into-csv-format/#findComment-360642 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.