SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    results of query to CSV file

    Is there a simple way to write to results of a sql query to a CSV file? I would guess there would be an inbuilt function to do this, but i cant find it.

    cheers for feedback
    Rascal, am I? Take that!

    -Errol Flynn

  2. #2
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you're using PHP 5.x you can try file_put_contents()
    PHP Code:
    file_put_contents('yourfile.csv'implode(';'$yourdata)); 


    if you're using 4.x you have to loop through your data.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2005
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    looping it is then. cheers frezno
    Rascal, am I? Take that!

    -Errol Flynn

  4. #4
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have the right permissions you could use the SELECT INTO OUTFILE syntax:

    Code:
    select * FROM table INTO OUTFILE
      "/tmp/file.csv" FIELDS TERMINATED By "," LINES TERMINATED BY "\n";
    You would of course have to escape commas that appeared in the data itself or Excel (or other program) will interpet them as field separators.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •