Jump to content

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-

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.