Note that there are some explanatory texts on larger screens.

plurals
  1. POdownload csv from database error
    text
    copied!<p>I have a database that I am downloading certain tables from like the staff table and an office table.</p> <p>I have the button to download it to a CSV file so it can be opened into Excel easily.</p> <p>This code works great when I'm on my local machine but the minute I upload it to a server I get warning messages that the header files have already been sent by another page and then the SQL statement dumps the requested information onto the page.</p> <p><strong>So here is the code I'm using:</strong></p> <p><strong>Button from main page:</strong></p> <pre><code>&lt;form method="get" action="export_myStaff.php"&gt; &lt;button type="submit" class="Mybutton"&gt;Download Staff Directory&lt;/button&gt; &lt;/form&gt; </code></pre> <p><strong>export_myStaff.php</strong></p> <pre><code>require_once('../Connections/myconnections.php'); // Connect to the database $link = mysql_connect($hostname, $username, $password); mysql_select_db($database_testSite ); require_once('csv_myStaff.php'); $staff_table="myStaff_tbl"; exportMysqlToCsv($staff_table); </code></pre> <p><strong>csv_myStaff.php</strong></p> <pre><code>function exportMysqlToCsv($staff_table) { $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\\"; $sql_query = "SELECT * FROM myStaff_tbl" // Gets the data from the database $result = mysql_query($sql_query); $fields_cnt = mysql_num_fields($result); $schema_insert = ''; for ($i = 0; $i &lt; $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 &lt; $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 &lt; $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"); $filename = $filename . "_" . date("Y-m-d_H-i", time()); header("Content-disposition: csv" . date("Y-m-d") . ".csv"); header("Content-disposition: filename=" . $filename . ".csv"); //header("Content-Disposition: attachment; filename=$filename"); echo $out; exit; } </code></pre> <p>Any ideas why it would work on my localhost but not on a server?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload