Exporting From MySQL to .csv using PHP

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

    Exporting From MySQL to .csv using PHP

    Hello,

    I'm sort of new to PHP. I am using the following code to retrieve a
    column from a database and to put all of the results into a .csv
    file. This allows creates a .csv file, but only lists the first
    result from the database. It is not returning all of the rows. I
    know the issue is somewhere in my "foreach" but I'm not sure where.

    Any help would be great. Thanks!

    <?php

    $selectSql = "SELECT artist_name FROM lounge_products ";
    $selects = $db->getAll($select Sql);

    foreach($select s as $select) {

    $content = $select[artist_name].",";

    }

    Header("Content-Disposition: attachment; filename=export .csv");
    print $content;
    ?>

  • =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

    #2
    Re: Exporting From MySQL to .csv using PHP

    mpar612 wrote:
    [...]only lists the first result from the database.
    Nope - it lists the last one.
    foreach($select s as $select) {
    >
    $content = $select[artist_name].",";
    You are destroying the previous contents of $content at every iteration.

    Try putting the header() up, and echoing things instead of putting them into
    variables, or try the .= operator.


    --
    ----------------------------------
    Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

    Un ordenador no es un televisor ni un microondas, es una herramienta
    compleja.

    Comment

    • Rik

      #3
      Re: Exporting From MySQL to .csv using PHP

      mpar612 <mpar612@gmail. comwrote:
      Hello,
      >
      I'm sort of new to PHP. I am using the following code to retrieve a
      column from a database and to put all of the results into a .csv
      file. This allows creates a .csv file, but only lists the first
      result from the database. It is not returning all of the rows. I
      know the issue is somewhere in my "foreach" but I'm not sure where.
      >
      Any help would be great. Thanks!
      >
      <?php
      >
      $selectSql = "SELECT artist_name FROM lounge_products ";
      $selects = $db->getAll($select Sql);
      >
      foreach($select s as $select) {
      >
      $content = $select[artist_name].",";
      Concat:
      $content .= $select[artist_name].",";

      In the current code you keep overwriting $concat with the new value.
      If you db object returns an array, this would be simpler:

      $content = implode(',',$se lects);

      Now, all this code will not help you if an artists name contains a comma,
      all your logic is lost.

      For creating CSV's, I'd use this:
      header("Content-Disposition: attachment; filename=export .csv");
      $selectSql = "SELECT artist_name FROM lounge_products ";
      $selects = $db->getAll($select Sql);
      $out = fopen('php://output','w');
      foreach($select s as $select) fputcsv($out,$s elect);



      Alternatively, use mysql's built in option to export a file, which would
      also take care of escaping:
      $file = '/path/to/file';
      mysql_query("SE LECT artist_name
      INTO OUTFILE '{$file}'
      FIELDS TERMINATED BY ','
      OPTIONALLY ENCLOSED BY '\"'
      LINES TERMINATED BY '\n'
      FROM lounge_products ");
      header("Content-Disposition: attachment; filename=export .csv");
      readfile($file) ;
      --
      Rik Wasmus
      Posted on Usenet, not any forum you might see this in.
      Ask Smart Questions: http://tinyurl.com/anel

      Comment

      • mpar612

        #4
        Re: Exporting From MySQL to .csv using PHP

        Excellent. Works perfectly. Thank you both for your help!

        Comment

        • Martin Mandl - m2m tech support

          #5
          Re: Exporting From MySQL to .csv using PHP

          On Mar 8, 12:57 am, Iván Sánchez Ortega <ivansanchez-...@rroba-
          escomposlinux.-.punto.-.orgwrote:
          mpar612 wrote:
          [...]only lists the first result from the database.
          >
          Nope - it lists the last one.
          >
          foreach($select s as $select) {
          >
          $content = $select[artist_name].",";
          >
          You are destroying the previous contents of $content at every iteration.
          >
          Try putting the header() up, and echoing things instead of putting them into
          variables, or try the .= operator.
          >
          --
          ----------------------------------
          Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-
          >
          Un ordenador no es un televisor ni un microondas, es una herramienta
          compleja.
          Dear mpar612,

          if you need your results in that that array change your code e.g. to:

          $i = 0;
          foreach($select s as $select) {
          $content[$i] = $select[artist_name].",";
          $i++;
          }

          Good luck
          Martin


          ------------------------------------------------
          online accounting on bash bases
          Online Einnahmen-Ausgaben-Rechnung

          ------------------------------------------------
          m2m server software gmbh


          Comment

          Working...