nepzap2 Posted July 21, 2009 Share Posted July 21, 2009 Do any of you know how to make a download link that will download MySQL data as CSV My code is below <?php session_start(); include("../db_config.php"); include("functions.php"); if("getcsv" === $_GET['step']){ $applicants = getAllApplicantInfo(); echo "LAST NAME, FIRST NAME, ASSIGNED PI, PARTICIPATION YEAR, STUDENT RANK\n"; while($row = mysql_fetch_array($applicants)){ echo mysql_real_escape_string( $row['last_name'] ) . ", " . mysql_real_escape_string($row['first_name'] ) . ", " . mysql_real_escape_string($row['pi_first_name'] ) . " " . mysql_real_escape_string($row['pi_last_name'] ) . ", " . "n/a". ", " . mysql_real_escape_string($row['total_points'] ) . "\n"; } return; } ?> any help is greatly appreciated Link to comment https://forums.phpfreaks.com/topic/166814-csv-download-link-help/ Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 edit: Requires PHP 5.1+ try this: <?php include("../db_config.php"); include("functions.php"); if("getcsv" === $_GET['step']){ $applicants = getAllApplicantInfo(); $out = fopen('php://output', 'w'); //Header fputcsv($out, array('LAST NAME','FIRST NAME','ASSIGNED PI','PARTICIPATION YEAR','STUDENT RANK')); while($row = mysql_fetch_array($applicants)){ fputcsv($out, array( $row['last_name'], $row['first_name'], $row['pi_first_name'], $row['pi_last_name'], "n/a", $row['total_points'], )); } fclose($out); return; } ?> Link to comment https://forums.phpfreaks.com/topic/166814-csv-download-link-help/#findComment-879654 Share on other sites More sharing options...
nepzap2 Posted July 21, 2009 Author Share Posted July 21, 2009 I pretty much just want to click on this link <?php echo "<a href='SummerInternStatsCSVTEST.php'>Download Table</a>"; ?> Have it just show the download box this is the function <?php function exportMysqlToCsv($table,$filename = 'export.csv') { $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "select * from $table"; // Gets the data from the database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; for ($i = 0; $i < $fields_cnt; $i++) { $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, stripslashes(mysql_field_name($result, $i))) . $csv_enclosed; $schema_insert .= $l; $schema_insert .= $csv_separator; } // end for $out = trim(substr($schema_insert, 0, -1)); $out .= $csv_terminated; // Format the data while ($row = mysql_fetch_array($result)) { $schema_insert = ''; for ($j = 0; $j < $fields_cnt; $j++) { if ($row[$j] == '0' || $row[$j] != '') { if ($csv_enclosed == '') { $schema_insert .= $row[$j]; } else { $schema_insert .= $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed; } } else { $schema_insert .= ''; } if ($j < $fields_cnt - 1) { $schema_insert .= $csv_separator; } } // end for $out .= $schema_insert; $out .= $csv_terminated; } // end while header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Length: " . strlen($out)); // Output to browser with appropriate mime type, you choose header("Content-type: text/x-csv"); //header("Content-type: text/csv"); //header("Content-type: application/csv"); header("Content-Disposition: attachment; filename=$filename"); echo $out; exit; } ?> and the actual file <?php $host = 'localhost'; // MYSQL database host adress $db = 'test'; // MYSQL database name $user = ''; // Mysql Datbase user $pass = ''; // Mysql Datbase password // Connect to the database $link = mysql_connect($host, $user, $pass); mysql_select_db($db); require 'exportcsv.inc.php'; $table="applicants"; // this is the tablename that you want to export to csv from mysql. exportMysqlToCsv($table); ?> but I keep getting this: Warning: Cannot modify header information - headers already sent by (output started at /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php:65) in /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php on line 50 Warning: Cannot modify header information - headers already sent by (output started at /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php:65) in /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php on line 51 Warning: Cannot modify header information - headers already sent by (output started at /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php:65) in /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php on line 53 Warning: Cannot modify header information - headers already sent by (output started at /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php:65) in /usr/local/apache/vhosts/boycethompson.com/Summer/internship/exportcsv.inc.php on line 56 Link to comment https://forums.phpfreaks.com/topic/166814-csv-download-link-help/#findComment-879666 Share on other sites More sharing options...
rhodesa Posted July 21, 2009 Share Posted July 21, 2009 Hum...i don't see anything wrong with it. Those errors come from content being outputted before the header() function...even a single space character. Check all your files for any extra whitespace before/after the php tags. also, if right before your first header() function, if you put this: print "DONE";exit; reload the page and do a View Source...is there anything before the DONE word (including any space characters) Link to comment https://forums.phpfreaks.com/topic/166814-csv-download-link-help/#findComment-879673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.