[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Export mysql query results to excel from a php page

Posted on 2007-07-24
2
Medium Priority
?
4,376 Views
Last Modified: 2013-12-12
I am trying to export mysql results from a php page to excel. I have been trying different code I have found online, but a consistent error I get is: Warning: Cannot modify header information - headers already sent by (output started at D:\Inetpub\wwwroot\dealernet\admin\download.php:1) in D:\Inetpub\wwwroot\dealernet\admin\download.php on line 18

Warning: Cannot modify header information - headers already sent by (output started at D:\Inetpub\wwwroot\dealernet\admin\download.php:1) in D:\Inetpub\wwwroot\dealernet\admin\download.php on line 19

Here is the coded page: (admin_eyedoo.php contains the connection string)
 <?
include ("admin_eyedoo.php");
$queryexport = "SELECT * FROM p_eventlog WHERE event = '$eventid'"; //second for list
                                    $export = mysql_query($queryexport) or die ("Error in query: $queryexport. " . mysql_error());
                                    
$tsv  = array();
$html = array();
while($row = mysql_fetch_array($export, 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;
//echo $html;

mysql_close($connection);
                                ?>
0
Comment
Question by:nisupport
2 Comments
 
LVL 14

Accepted Solution

by:
Aamir Saeed earned 2000 total points
ID: 19558603
actually you should not send any output to browser before header() thats why this error is coming. output may be some space, empty line, echo or print statements but if you need to send data before header(), you can use ob_start() function in the start and ob_end_flush() in the end to avoid this error, please see manual for more details and also have a look of this code to export data to excel

<?php
      $str_transaction_qry = "SELECT * FROM ".$user_prefix."_users";
      $user_excel = 'Users Points';

      $get_result = mysql_query($str_transaction_qry);
      $data .= " \n\n                          $user_excel \n\n";
      $data .= "Username, Forums, Banner Clicks, Submit New Business, Vote Survey, Reviews, Rated, Recommend, Total Points\n";
      
      $num_record = mysql_num_rows($get_result);

      if($num_record >= 1){
            while($obj = mysql_fetch_object($get_result)){
                  
                  $user_name = $obj->username;
                  
                  $user_overall_points = $obj->user_topics * 3 + $obj->user_posts * 5;
                  
                  $reco = $obj->user_reco * 10;
                  
                  $user_clicks = $obj->clicks;
                  
                  $user_revi = $obj->user_reviews * 7;
                  
                  $user_rati = $obj->user_rating * 5;
                  
                  $sub_new_bus = $obj->sub_new_bus * 15;
                  
                  $survey_vote = $obj->survey_vote * 5;
                  
                  $user_tot_points = $user_overall_points + $reco + $user_clicks + $user_revi + $user_rati + $sub_new_bus + $survey_vote;
                  
                  $data .= "\"".$user_name."\",\"".$user_overall_points."\",\"".$user_clicks."\",\"".$sub_new_bus."\",\"".$survey_vote."\",\"".$user_revi."\",\"".$user_rati."\",\"".$reco."\",\"".$user_tot_points."\"\n";
            
            }
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment;filename=report.csv");
            header("Pragma: no-cache");
            header("Expires: 0");
            print($data);                         
      }
?>
0
 
LVL 2

Expert Comment

by:eechincs
ID: 19569341
Hi nisupport,
As I know, the error you get is cause by before you write these two line:
    header("Content-type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$fileName");
the header information already been set. Make sure only set the header information for once, then no such error message will be prompt out.

Regards,
eechincs
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question