By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Saving two mysql tables as csv files

P: n/a
The following code only generates the first csv file. The second
request is just ignored. What am I doing wrong? I am using Mozilla
Firefox, Windows XP, and Xampp.

<?php

require 'bm_connect.php';

// export to csv file the table bmbookmark and call it bookmarks.csv
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=bookmarks.csv");
$query = "SELECT * FROM bmbookmark";
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
print implode(",", $row)."\n";
};
mysql_free_result($result);
exit();

// export to csv file the table bmcat and call it categories.csv
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=categories.csv");
$query = "SELECT * FROM bmcat";
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
print implode(",", $row)."\n";
};
mysql_free_result($result);
exit();

?>

Regards,
Jan Nordgreen
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
While the city slept, Jan Nordgreen <ro*******@hotmail.com> feverishly
typed:
The following code only generates the first csv file. The second
request is just ignored. What am I doing wrong? I am using Mozilla
Firefox, Windows XP, and Xampp.

<?php

require 'bm_connect.php';

// export to csv file the table bmbookmark and call it bookmarks.csv
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=bookmarks.csv");
$query = "SELECT * FROM bmbookmark";
$result = mysql_query($query);
while($row = mysql_fetch_row($result)){
print implode(",", $row)."\n";
};
mysql_free_result($result);
exit();

[...]

Do you really want the script to exit here? Or would you prefer it to carry
on and do the second csv file? ;-) If so, take out this first instance of
exit();

Cheers,
Nige

--
Nigel Moss.

Email address is not valid. ni***@nigenetDOG.org.uk. Take the dog out!
http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
In the land of the blind, the one-eyed man is very, very busy!
Jul 17 '05 #2

P: n/a
So that is what exit(); does! :)

When I removed the exit(); only one csv file was created.

First the first table was listed,

then this message was listed in the csv file:

<b>Warning</b>: Cannot modify header information - headers already
sent by (output started at C:\jans data\jan programs\apache php mysql
etc\xampp\htdocs\xampp\excel.php:31) in <b>C:\jans data\jan
programs\apache php mysql etc\xampp\htdocs\xampp\excel.php</b> on line
<b>37</b><br />

and another warning:

<b>Warning</b>: Cannot modify header information - headers already
sent by (output started at C:\jans data\jan programs\apache php mysql
etc\xampp\htdocs\xampp\excel.php:31) in <b>C:\jans data\jan
programs\apache php mysql etc\xampp\htdocs\xampp\excel.php</b> on line
<b>38</b><br />

then the second table was listed,

then the html code of the main php file was listed.

I wanted two csvs, not one. I don't like error messages in the csv
files. I don't like the html code listed in the csv file.

What am I doing wrong?

Regards,
Jan Nordgreen
Jul 17 '05 #3

P: n/a

"Jan Nordgreen" <ro*******@hotmail.com> wrote in message
news:34**************************@posting.google.c om...
So that is what exit(); does! :)

When I removed the exit(); only one csv file was created.

First the first table was listed,

then this message was listed in the csv file:

<b>Warning</b>: Cannot modify header information - headers already
sent by (output started at C:\jans data\jan programs\apache php mysql
etc\xampp\htdocs\xampp\excel.php:31) in <b>C:\jans data\jan
programs\apache php mysql etc\xampp\htdocs\xampp\excel.php</b> on line
<b>37</b><br />

and another warning:

<b>Warning</b>: Cannot modify header information - headers already
sent by (output started at C:\jans data\jan programs\apache php mysql
etc\xampp\htdocs\xampp\excel.php:31) in <b>C:\jans data\jan
programs\apache php mysql etc\xampp\htdocs\xampp\excel.php</b> on line
<b>38</b><br />

then the second table was listed,

then the html code of the main php file was listed.

I wanted two csvs, not one. I don't like error messages in the csv
files. I don't like the html code listed in the csv file.

What am I doing wrong?

Regards,
Jan Nordgreen


You're trying to download two documents to the user - that's what's wrong.
You can't. Aa a suggestion, use Javascript to open a window for each
download (even if that will be ugly).

Garp
Jul 17 '05 #4

P: n/a
While the city slept, Jan Nordgreen <ro*******@hotmail.com> feverishly
typed:
So that is what exit(); does! :)
It does exactly what it says on the tin! ;-)
When I removed the exit(); only one csv file was created.


Sorry. I didn't read your code properly. I can see what it is doing now.

As an alternative, you *could* produce a script to write the data from the
tables into two csv files on the server (see
http://www.php.net/manual/en/ref.filesystem.php for some info on PHP's file
handling functions), then make a link to each file on the page, or even then
open a page that presents each of the two files in <spit>frames</spit>...
just some thoughts.

Hope that helps,
Nige

--
Nigel Moss.

Email address is not valid. ni***@nigenetDOG.org.uk. Take the dog out!
http://www.nigenet.org.uk | Boycott E$$O!! http://www.stopesso.com
In the land of the blind, the one-eyed man is very, very busy!
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.