Saving two mysql tables as csv files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Nordgreen

    Saving two mysql tables as csv files

    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=bookma rks.csv");
    $query = "SELECT * FROM bmbookmark";
    $result = mysql_query($qu ery);
    while($row = mysql_fetch_row ($result)){
    print implode(",", $row)."\n";
    };
    mysql_free_resu lt($result);
    exit();

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

    ?>

    Regards,
    Jan Nordgreen
  • nice.guy.nige

    #2
    Re: Saving two mysql tables as csv files

    While the city slept, Jan Nordgreen <room23111@hotm ail.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=bookma rks.csv");
    > $query = "SELECT * FROM bmbookmark";
    > $result = mysql_query($qu ery);
    > while($row = mysql_fetch_row ($result)){
    > print implode(",", $row)."\n";
    > };
    > mysql_free_resu lt($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@nigenetDO G.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!


    Comment

    • Jan Nordgreen

      #3
      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\htdoc s\xampp\excel.p hp:31) in <b>C:\jans data\jan
      programs\apache php mysql etc\xampp\htdoc s\xampp\excel.p hp</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\htdoc s\xampp\excel.p hp:31) in <b>C:\jans data\jan
      programs\apache php mysql etc\xampp\htdoc s\xampp\excel.p hp</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

      Comment

      • Garp

        #4
        Re: Saving two mysql tables as csv files


        "Jan Nordgreen" <room23111@hotm ail.com> wrote in message
        news:3488b88c.0 406081358.2df37 a77@posting.goo gle.com...[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\htdoc s\xampp\excel.p hp:31) in <b>C:\jans data\jan
        > programs\apache php mysql etc\xampp\htdoc s\xampp\excel.p hp</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\htdoc s\xampp\excel.p hp:31) in <b>C:\jans data\jan
        > programs\apache php mysql etc\xampp\htdoc s\xampp\excel.p hp</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


        Comment

        • nice.guy.nige

          #5
          Re: Saving two mysql tables as csv files

          While the city slept, Jan Nordgreen <room23111@hotm ail.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@nigenetDO G.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!


          Comment

          Working...