I have a script that I have modified to export a MySQL table to an Excel File. The script works except for the fact that it duplicates every element.
For example, if I have a record that consists of:
firstName = John
lastName = Doe
email = john@email.com
The output will be:
John John Doe Doe john@email.com john@email.com
It will do this for every record in the table. Here is the code:
//Config file holds all connection information
require_once('config.php');
// Connect to DB
$conn = mysql_connect(LOCATION,USERNAME,PASSWORD);
if (!$conn) die ("Cannot connect MySQL");
// execute connection string
mysql_select_db(DATABASE,$conn) or die ("Could not open database");
$result = mysql_query("select * from tblContacts");
$tsv_output = array();
while($row = mysql_fetch_array($result)) {
$tsv_output[] = implode("\t", $row);
}
$tsv_output = implode("\r\n", $tsv_output);
$fileName = 'registrations.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");
echo $tsv_output;