CSV output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Paul

    CSV output


    I need to write the output of an SQL query as comma-separated values.
    So I wrote this, which is almost right:

    while($row = mysql_fetch_arr ay($rs, MYSQL_ASSOC))
    {
    $rows[] = $row;
    }
    foreach( $rows[0] as $key =$value)
    {
    echo $key.",";
    }
    echo "\n";

    foreach ($rows as $row)
    {
    foreach( $row as $key =$value)
    {
    echo $value.",";
    }
    echo "\n";
    }


    It first writes a header row, then all the data rows below it. The only
    problem is the trailing comma on each line. Is there some clever way of
    knowing when I'm on the last element, and so not add the last comma?

    If it's not already blindingly obvious, I'm new to PHP ;-)

    Thanks,
    -Steve
  • Geoff Berrow

    #2
    Re: CSV output

    Message-ID: <19102006172204 4530%listcatche r@fastOUTmail.f mfrom Steven
    Paul contained the following:
    >It first writes a header row, then all the data rows below it. The only
    >problem is the trailing comma on each line. Is there some clever way of
    >knowing when I'm on the last element, and so not add the last comma?


    --
    Geoff Berrow (put thecat out to email)
    It's only Usenet, no one dies.
    My opinions, not the committee's, mine.
    Simple RFDs http://www.ckdog.co.uk/rfdmaker/

    Comment

    • Steven Paul

      #3
      Re: CSV output

      In article <q73gj21605jvjv ghpkshdva6nfj7i t1s81@4ax.com>, Geoff Berrow
      <blthecat@ckdog .co.ukwrote:

      Is there some clever way of
      knowing when I'm on the last element, and so not add the last comma?
      >
      http://uk2.php.net/implode
      Wow, this PHP has everything ;-)

      Thanks Geoff.

      Comment

      • Colin Fine

        #4
        Re: CSV output

        Steven Paul wrote:
        In article <q73gj21605jvjv ghpkshdva6nfj7i t1s81@4ax.com>, Geoff Berrow
        <blthecat@ckdog .co.ukwrote:
        >
        >
        >>Is there some clever way of
        >>knowing when I'm on the last element, and so not add the last comma?
        >http://uk2.php.net/implode
        >
        Wow, this PHP has everything ;-)
        >
        Thanks Geoff.
        It got it from Perl! ;-)

        Colin

        Comment

        • Marcin Dobrucki

          #5
          Re: CSV output



          Steven Paul wrote:
          I need to write the output of an SQL query as comma-separated values.
          So I wrote this, which is almost right:
          >
          while($row = mysql_fetch_arr ay($rs, MYSQL_ASSOC))
          {
          $rows[] = $row;
          }
          foreach( $rows[0] as $key =$value)
          {
          echo $key.",";
          }
          echo "\n";
          >
          foreach ($rows as $row)
          {
          foreach( $row as $key =$value)
          {
          echo $value.",";
          }
          echo "\n";
          }
          You are reinventing the wheel:

          SELECT * INTO OUTFILE "/tmp/outfile.csv" FIELDS TERMINATED BY "," FROM
          <yourtable>;

          There are also a number of options on enclosing fields, specifying
          newlines and so on.

          /m

          Comment

          • Steven Paul

            #6
            Re: CSV output

            In article <9bn%g.40872$Nb 2.770734@news1. nokia.com>, Marcin Dobrucki
            <Marcin.Dobruck i@TAKETHISAWAY. nokia.comwrote:
            SELECT * INTO OUTFILE "/tmp/outfile.csv" FIELDS TERMINATED BY "," FROM
            <yourtable>;
            Thanks. I didn't know about that.

            Comment

            Working...