As part of my website i want to be able to allow mysql table to be downloaded to excel I have the below code to 'get' the data :
<?php
require_once("config.php");
$db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password);
mysql_select_db($AddressBook_DatabaseName,$db);
$query = "SELECT Name, DOB, HouseNumber FROM Addresses";
$result = mysql_query($query) or die('Error, query failed');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "Name :{$row['Name']} <br" .
"DOB : {$row['DOB']} <br" .
"HouseNumber : {$row['HouseNumber']} <br><br>";
}
mysql_free_result($result);
include 'close.php';
?>
The below code then takes the data and allows to be converted to excel:
<?php
include 'download.php';
$query = "SELECT Name, DOB, HoseNumber FROM Addresses";
$result = mysql_query($query) or die('Error, query failed');
$tsv = array();
$html = array();
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
$tsv[] = implode("\t", $row);
$html[] = "<tr><td>" .implode("</td><td>", $row) . "</td></tr>";
}
$tsv = implode("\r\n", $tsv);
$html = "<table>" . implode("\r\n", $html) . "</table>";
$fileName = 'mysql-to-excel.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
echo $tsv;
include 'download.php';
?>
All it does is give the below output and it just displays in the web page not in excel ? what have i done wrong ?
Name :
Name :
522380 Ptechnology group alivemore 358225 eservices person a