Jump to content

Recommended Posts

I have followed a tutorial on phpFreaks http://www.phpfreaks.com/tutorials/114/0.php to export mysql table data to ms excel. It prints the data to the screen but doesn't prompt for download.

Here's my code:

 

<?php

 

$user="user";

  $host="localhost";

  $password="pass";

  $database = "db";

  $connection = mysql_connect($host,$user,$password)

  or die ("couldn't connect to server");

  if ($db = mysql_select_db($database,$connection))

      echo"connection successful";

     

if (check_valid_user_admin())

{

 

$select = "SELECT * FROM tbl_applicants";               

$export = mysql_query($select);

$fields = mysql_num_fields($export);

 

for ($i = 0; $i < $fields; $i++) {

    $header .= mysql_field_name($export, $i) . "\t"; }

 

while($row = mysql_fetch_row($export)) {

    $line = '';

    foreach($row as $value) {                                           

        if ((!isset($value)) OR ($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 = "\n(0) Records Found!\n";                       

}

$suffix = date(dmy);

header("Content-type: application/x-msdownload");

header("Content-Disposition: attachment; filename=cap_roster_export.xls");

header("Pragma: no-cache");

header("Expires: 0");

print "$header\n$data"; 

 

}

 

?>     

 

All the data looks like it's formatted well but just shows up on the page and never prompts for download.

 

This question was already asked by someone, but nobody answered it...

Please help me!

Thanks in advance,

Olivier

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.