Bruce Gust
asked on
I've got things out of sequence here. What needs to change?
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:
Cannot modify header information - headers already sent by (output started at C:\wamp\www\json\csv_test.
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);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Note the missing "c" in application.
header('Content-type:appliation/csv' );
header('Content-type:application/csv');
Note the missing "c" in application.
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!