Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I've got things out of sequence here. What needs to change?

Posted on 2014-09-25
4
Medium Priority
?
131 Views
Last Modified: 2014-09-29
I'm generating a csv file and when it finishes, I'm getting an error that reads:

Cannot modify header information - headers already sent by (output started at C:\wamp\www\json\csv_test.php:37) in C:\wamp\www\json\csv_test.php on line 40

Which makes total sense since I'm echoing the output just before that.

But how do I get it to the actual csv file. Stupid question, but there it is....

Here's my code:

<?php 
ini_set("memory_limit","500M");
error_reporting(E_ALL);

set_time_limit(600);
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");

$output="";

$michelle="select * from verizon order by id";
$michelle_query=mysqli_query($cxn, $michelle)
or die("Michelle didn't work");
$michelle_columns=mysqli_field_count($cxn);

//gets the field names from your table and sets them up as the first row in your csv file

$heading=mysqli_fetch_fields($michelle_query);

foreach ($heading as $val)
{
	$output .='"'.$val->name .'",';
}
$output .="\n";


while($michelle_row=mysqli_fetch_array($michelle_query))
{
	for($i=0; $i<$michelle_columns; $i++)
	{
		$output .='"'.$michelle_row["$i"].'",';
	}
$output .="\n";
}

echo $output;

$filename="json.csv";
header('Content-type:appliation/csv');
header('Content-Disposition: attachment; filename='.$filename);

?>

Open in new window

0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 40344893
You probably want to modify the flow of the script's logic to send the headers before any of the output.  Maybe something like this.  Sorry I cannot test this because i do not have your data set.

set_time_limit(600);
include ("carter.inc");
$cxn = mysqli_connect($host,$user,$password,$database)
or die ("couldn't connect to server");

$output="";

$michelle="select * from verizon_test order by id";
$michelle_query=mysqli_query($cxn, $michelle)
or die("Michelle didn't work");
$michelle_columns=mysqli_field_count($cxn);

$filename="json.csv";

// OPEN THE CSV FILE HERE
$file = fopen($filename, 'w');

// GET THE FIELD NAMES
$heading=mysqli_fetch_fields($michelle_query);
fputcsv($file, $heading);

// GET THE DATA SET
while($michelle_row=mysqli_fetch_assoc($michelle_query))
{
    fputcsv($file, $michelle_row);
}

// REWIND THE CSV FILE HERE
rewind($file);

// SEND HEADERS AND DATA
header('Content-type:application/csv');
header('Content-Disposition: attachment; filename='.$filename);

fpassthru($file);

Open in new window

0
 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1000 total points
ID: 40344896
Just move the echo on line 37 underneath the last header()
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40344908
Also, one of the core habits of successful software developers is reading the fine manuals.  For example, look up the PHP man page for this function: mysqli_fetch_array()
http://php.net/manual/en/mysqli-result.fetch-array.php

Once you have read that man page, if you do not understand why you should not be using that function here, please post back and we can try to help.  But I think you will agree that it is obvious that mysqli_fetch_array() returns twice as much data as you want!
0
 
LVL 8

Expert Comment

by:Jim Riddles
ID: 40345261
You also have a typo on line 40 of your code, which Ray already corrected in his example.  I just wanted to point it out for you.

header('Content-type:appliation/csv');

header('Content-type:application/csv');

Open in new window


Note the missing "c" in application.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

688 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