Jump to content

Table to excel not working


ady01

Recommended Posts

I have written the below to download my data from MySQL to excel format however all it does is display my error  'SQL table is empty, no data to display. Please add entries to database before using the download function'

 

Any idears why this is not working for me  ?

 

 

 

<?php
include('config.php');

$db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password);
mysql_select_db($AddressBook_DatabaseName,$db);

$result = mysql_query('select * from Addresses', $linkID);
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
    $header .= mysql_field_name($result, $i)."\t";
}

while($row = mysql_fetch_row($result)){
  $line = '';
  foreach($row as $value){
    if(!isset($value) || $value == ""){
      $value = "\t";
    }else{

      $value = str_replace('"', '""', $value);

      $value = '"' . $value . '"' . "\t";
    }
    $line .= $value;
  }
  $data .= trim($line)."\n";
}

  $data = str_replace("\r", "", $data);



if ($data == "") {
  $data = "\nSQL table is empty, no data to display. Please add entries to database before using the download function\n";
}


header("Content-type: application/octet-stream");


header("Content-Disposition: attachment; filename=buddylist.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data;
?> 

Link to comment
https://forums.phpfreaks.com/topic/105025-table-to-excel-not-working/
Share on other sites

That seems to have sorted the error however the dat is now comming out all skew wiff - the below is what im getting in html not downloading to excel like i want ?

 

 

ID Name DOB HouseNumber Street City Country Telephone Fax Email Remarks "50" "522380" "Ptechnology group" "alivemore" "tgoater" "[email protected]" "example" "example" "Joanne A" "[email protected]" "None" "51" "358225" "eservices" "person a" "Person 1" "[email protected]" "example" "example" "Robert" "[email protected]" "Place order Via EDI link"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.