Jump to content

Export mysql table to CVS


kristian_gl

Recommended Posts

Hey guys, I'm trying to export a table in a mysql database. But i can't seem to get it to work. Instead of a "save as - popup message" when I run the code I get some errors:

 

Notice: Undefined variable: csv_output in /home/stud/oddl/public_html/admin/export.php  on line 22

 

Warning: Cannot modify header information - headers already sent by (output started at /home/stud/oddl/public_html/admin/exporter.php:2) in /home/stud/oddl/public_html/admin/export.php on line 38

 

Warning: Cannot modify header information - headers already sent by (output started at /home/stud/oddl/public_html/admin/exporter.php:2) in /home/stud/oddl/public_html/admin/export.php on line 39

 

Warning: Cannot modify header information - headers already sent by (output started at /home/stud/oddl/public_html/admin/exporter.php:2) in /home/stud/oddl/public_html/admin/export.php on line 40

 

And the content of the table shows in export.php, but it will not automatically save to a CVS.file. I have also tried to put this:

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");

 

at the top of the document, and managed to remove the: "Warning: Cannot modify header information" errors, but with still no saving to CVS.

 

CODE:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$host = 'db.stud.aitel.hist.no';
$user = 'oddl';
$pass = 'EgjdlAQk';
$db = 'oddl';
$table = 'svar';
$file = 'test';

$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("d-m-Y_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;
?>

 

 

Kristian

Link to comment
https://forums.phpfreaks.com/topic/200667-export-mysql-table-to-cvs/
Share on other sites

Warning: Cannot modify header information - headers already sent by (output started at /home/stud/oddl/public_html/admin/exporter.php:2) in /home/stud/oddl/public_html/admin/export.php on line 38

 

Warning: Cannot modify header information - headers already sent by (output started at /home/stud/oddl/public_html/admin/exporter.php:2) in /home/stud/oddl/public_html/admin/export.php on line 39

 

Warning: Cannot modify header information - headers already sent by (output started at /home/stud/oddl/public_html/admin/exporter.php:2) in /home/stud/oddl/public_html/admin/export.php on line 40

http://www.phpfreaks.com/forums/index.php/topic,37442.0.html

 

Take a look at the stickies before posting.

 

-cb-

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.