Database Forum
Web Database Register Members List Calendar FAQ

 
Go Back   Database Forum > Database Management > PHP & MySQL
User Name
Password

Reply
 
Thread Tools Search this Thread Display Modes
Default Problem saving csv data from MySql database
11-26-2008, 06:53 AM #1  

BE55Roberts
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.
Reply With Quote

Default
11-28-2008, 05:45 PM #2  

SusanH
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.
Reply With Quote

Angry Still having trouble saving CSV file
12-15-2008, 06:46 AM #3  

BE55Roberts
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.
Reply With Quote

Default
12-15-2008, 02:37 PM #4  

SusanH
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..
Reply With Quote

Default How do you remove prepended whitespace?
12-29-2008, 03:58 AM #5  

BE55Roberts
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?

Reply With Quote

Default
12-29-2008, 11:55 AM #6  

Sandman
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
Reply With Quote

Thumbs up Problem resolved and Great Example
01-12-2009, 09:54 AM #7  

BE55Roberts
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
Reply With Quote

Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



All times are GMT -5. The time now is 05:16 AM.

Powered by: vBulletin Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Database Forums

Help support our forum assistants and product development ->

go to top go to top