Well I have tried many things to get this working and am completley stuck - I want to take my data from MySQL and download it to excel format using php on my website - This is the code i have but its outputting to the browser window & not excel as i want, can anyone help as to why this s not working for me ?

<?php

include("config.php");
mysql_connect($AddressBook_HOST, $AddressBook_Username, $AddressBook_Password) or die ("Can't connect!"); 
mysql_select_db($AddressBook_DatabaseName) or die ("Can't open database!"); 

$XML = "ID,Name,DOB,HouseNumber,Street,City,Country,Telephone,Fax,Email,Remarks\n";
$file ="report_". date("Y-m-d"). ".txt";

$query = "SELECT * FROM Addresses ORDER by ID ASC";
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
while($myrow = mysql_fetch_array($result)) {
    $XML.= $myrow[ID]. "\t";
    $XML.= $myrow[Name]. "\t";
    $XML.= $myrow[DOB]. "\t";
	$XML.= $myrow[HouseNumber]. "\t";
	$XML.= $myrow[Street]. "\t";
	$XML.= $myrow[City]. "\t";
	$XML.= $myrow[Country]. "\t";
	$XML.= $myrow[Telephone]. "\t";
	$XML.= $myrow[Fax]. "\t";
	$XML.= $myrow[Email]. "\t";
    $XML.= $myrow[Remarks]. "\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Transfer-Encoding: binary");
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')){
    header('Cache-Control: public');
}
echo $XML;
exit;
?>

There are no errors comming up when its run, but it out puts the below to the browser window : (the data below is just test data inserted into Mysql)

ID,Name,DOB,HouseNumber,Street,City,Country,Telephone,Fax,Email,Remarks 50 522380 Ptechnology group alivemore tgoater tgoater@example.com example example Joanne A jahern@example.com None 51 358225 eservices person a Person 1 example@example.com example example Robert Rd@example.com Place order Via EDI link 54 test test test test test test test test test testing server 57 new new new new new new new new new new testing

    I can't remember where or when but I found this online once upon a time.

    header("Content-Type: application/vnd.ms-excel");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=NAME_OF_FILE.xls ")

      Try:

      <?php
      
      include("config.php");
      mysql_connect($AddressBook_HOST, $AddressBook_Username, $AddressBook_Password) or die ("Can't connect!"); 
      mysql_select_db($AddressBook_DatabaseName) or die ("Can't open database!"); 
      
      $XML = "ID,Name,DOB,HouseNumber,Street,City,Country,Telephone,Fax,Email,Remarks\n";
      $file ="report_". date("Y-m-d"). ".csv";
      
      $query = "SELECT * FROM Addresses ORDER by ID ASC";
      $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
      while($myrow = mysql_fetch_array($result)) {
          $XML.= $myrow[ID]. ",";
          $XML.= $myrow[Name]. ",";
          $XML.= $myrow[DOB]. ",";
      	$XML.= $myrow[HouseNumber]. ",";
      	$XML.= $myrow[Street]. ",";
      	$XML.= $myrow[City]. ",";
      	$XML.= $myrow[Country]. ",";
      	$XML.= $myrow[Telephone]. ",";
      	$XML.= $myrow[Fax]. ",";
      	$XML.= $myrow[email]. ",";
          $XML.= $myrow[Remarks]. "\n";
      }
      header("Content-type: application/octet-stream");
      header("Content-Disposition: attachment; filename=\"$file\"");
      header("Content-Transfer-Encoding: binary");
      if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE')){
          header('Cache-Control: public');
      }
      echo $XML;
      exit;
      ?>
      
        2 months later

        [MOD]Please do not re-open multiple old threads to promote your software.[/MOD]

          Write a Reply...