 |
 |
 |
Problem saving csv data from MySql database
 |
 |
 |
|
Junior Member
|
Status: Offline
Posts: 4
Join Date: Nov 2008
|
|
I enabled the Save CSV option, and then encountered the following error:
Warning: Cannot modify header information - headers already sent by (output started at config.php:24) in datalist.php on line 548
Any ideas on what is causing this? I do have a customized header.php, but tried reverting to the default one.
|
|
|
|
|
Senior Member
|
Status: Offline
Posts: 115
Join Date: Mar 2005
Location: CA
|
|
"Any ideas on what is causing this?"
One that comes to mind is possibly whitespace in the file you are executing. Maybe you made some edits recently.
|
|
|
|
 |
Still having trouble saving CSV file
 |
 |
 |
|
Junior Member
|
Status: Offline
Posts: 4
Join Date: Nov 2008
|
|
Checked the file. Nothing looks amiss there. I tried another table - all defaults. I still have the problem saving CSV file, and this is a simple table.
|
|
|
|
|
Senior Member
|
Status: Offline
Posts: 115
Join Date: Mar 2005
Location: CA
|
|
Do you have an application like excel on your pc? If so do you have an application defined to open csv files? MyComputer > Folder Options > Filetypes..
|
|
|
|
 |
How do you remove prepended whitespace?
 |
 |
 |
|
Junior Member
|
Status: Offline
Posts: 4
Join Date: Nov 2008
|
|
Checked and I do have CSV files set to open in Excel. I would expect that if MS Office is installed. However, I am not getting to that stage in the processing.
The error appears immediately, as the data is getting generated. From other posts, I suspect that I have whitespace in the output ahead of the header() call, so the browser is throwing up its hands. But of course, you can't see the whitespace. How do you remove prepended whitespace in a CSV file request?

|
|
|
|
|
Moderator
|
Status: Offline
Posts: 98
Join Date: Mar 2005
|
|
Simple script to export mysql table to csv file.
PHP Code:
<?php
$host = 'localhost';
$user = 'mysqlUser';
$pass = 'myUserPass';
$db = 'myDatabase';
$table = 'products_info';
$file = 'export';
$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field']."; ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j]."; ";
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>
Cheers 
|
|
|
|
 |
 |
 |
Problem resolved and Great Example
 |
 |
 |
|
Junior Member
|
Status: Offline
Posts: 4
Join Date: Nov 2008
|
|
Thanks, Sandman; your example works very well.  And it got me to thinking, "what is the difference between this simple example of a CSV export and what I'm doing?" I reviewed my set of required files, and I found the culprit! I had 2 blank lines in my configuration file. That was it. When I took those two blank lines out, the problem was resolved. Now the CSV button works for any page.
Last edited by BE55Roberts; 01-12-2009 at 09:57 AM.. Reason: stylistic trivia
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:16 AM.
|
 |
 |
|
|