Why Am I Getting HTML In My MySQL Export To CSV?

- 1 answer

I know this question has been asked before, but I ran into a problem.

Oddly enough, when I execute this function, it includes the html of the page that the link you select to execute the function.

function exportCSV($table) {
    $result = mysql_query("SHOW COLUMNS FROM ".$table."");
    $i = 0;
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $csv_output .= $row['Field']."; ";
    $csv_output .= "\n";

    $values = mysql_query("SELECT * FROM ".$table."");
    while ($rowr = mysql_fetch_row($values)) {
        for ($j=0;$j<$i;$j++) {
            $csv_output .= $rowr[$j]."; ";
        $csv_output .= "\n";

    $filename = $file."_".date("Y-m-d_H-i",time());
    header("Content-type: application/");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header( "Content-disposition: filename=".$filename.".csv");
    print $csv_output;

Does anyone know why it would include the HTML in the csv file? alt text



My guess is that you've got some sort of template that generates the same HTML header and footer regardless of the page that is requested. Sometime before the exportCSV function is called, the header is generated.

You don't show the bottom of the output, but I'll bet the footer is there too, since I suspect the flow control will continue on to that code after the function exits.