Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export mySQL Data to Excel - Warning: Cannot modify header information - headers already sent by 1

Status
Not open for further replies.

evil1966

MIS
Dec 2, 2013
57
US
I'm trying to export table data to excel. I get the "Cannot modify header information warning" on all three header lines at the end of the code and it dumps all the data to the screen not a file.

Code:
 <?php
  	$username="user";
	$host="myhost";
	$password="password";
	$database="mydatabase";
        $table="donors";
	
	
$con = mysql_connect($host,$username,$password);
    if(!$con)
    {
	 die("Unable to select database");
    }
    mysql_select_db($database,$con);
		
$select = "SELECT * FROM donors";

//run mysql query and then count number of fields
$export = mysql_query ( $select ) 
       or die ( "Sql error : " . mysql_error( ) );
$fields = mysql_num_fields ( $export );

//create csv header row, to contain table headers 
//with database field names
for ( $i = 0; $i < $fields; $i++ ) {
	$header .= mysql_field_name( $export , $i ) . ",";
}

//this is where most of the work is done. 
//Loop through the query results, and create 
//a row for each
while( $row = mysql_fetch_row( $export ) ) {
	$line = '';
	//for each field in the row
	foreach( $row as $value ) {
		//if null, create blank field
		if ( ( !isset( $value ) ) || ( $value == "" ) ){
			$value = ",";
		}
		//else, assign field value to our data
		else {
			$value = str_replace( '"' , '""' , $value );
			$value = '"' . $value . '"' . ",";
		}
		//add this field value to our row
		$line .= $value;
	}
	//trim whitespace from each row
	$data .= trim( $line ) . "\n";
}
//remove all carriage returns from the data
$data = str_replace( "\r" , "" , $data );


//create a file and send to browser for user to download
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$file_name.".csv");
print "$header\n$data";
exit;
?>

Thanks
 
you cannot output anything to screen before issuing the header commands.

Make sure you have nothing prior to your PHP code that is creating output. Even white space is taken as output.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Found it! Not only white space before the start php tag, but some extra blank lines in the code. I also added <? ob_start(); ?> and <? ob_flush(); ?> to the top and bottom of my page.

Thanks!
 
Welcome.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top