Connecting Tech Pros Worldwide Help | Site Map

Saving two mysql tables as csv files

Jan Nordgreen
Guest
 
Posts: n/a
#1: Jul 17 '05
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
nice.guy.nige
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Saving two mysql tables as csv files


While the city slept, Jan Nordgreen <room23111@hotmail.com> feverishly
typed:
[color=blue]
> 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();[/color]
[...]

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. nigel@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!


Jan Nordgreen
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Saving two mysql tables as csv files


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
Garp
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Saving two mysql tables as csv files



"Jan Nordgreen" <room23111@hotmail.com> wrote in message
news:3488b88c.0406081358.2df37a77@posting.google.c om...[color=blue]
> 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[/color]

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


nice.guy.nige
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Saving two mysql tables as csv files


While the city slept, Jan Nordgreen <room23111@hotmail.com> feverishly
typed:
[color=blue]
> So that is what exit(); does! :)[/color]

It does exactly what it says on the tin! ;-)
[color=blue]
> When I removed the exit(); only one csv file was created.[/color]

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. nigel@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!


Closed Thread