Closed Thread
Results 1 to 7 of 7

Thread: MySQL Export to csv

  1. #1
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    MySQL Export to csv

    Hi all

    I'm trying to code a data export function but having one simple problem.

    mysql_connect and select db has been done in seperate connection file

    First off, I've created a recordset called $RScontacts.

    PHP Code:
    $out ''

    // Get all fields names from table.
    $fields mysql_list_fields($database_freecrm,CONTACTS);

    // Count the table fields and put the value into $columns. 
    $columns mysql_num_fields($fields);


    // Put the name of all fields to $out. 
    for ($i 0$i $columns$i++) {
    $l=mysql_field_name($fields$i);
    $out .= '"'.$l.'",';
    }
    $out .="n";

    // Add all values in the table to $out. 
    while ($l mysql_fetch_array($RScontacts)) {
    for (
    $i 0$i $columns$i++) {
    $out .='"'.$l["$i"].'",';
    }
    $out .="n";
    }

    // Open file export.csv.
    $f fopen ('export.csv','w');

    // Put all values from $out to export.csv. 
    fputs($f$out);
    fclose($f);

    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename="Contact_Export.csv"');
    readfile('export.csv'); 
    It works great, BUT - each new row isn't being defined in the csv file so I get one long row with all table row data. - no new lines.


    Am I going about this the wrong way?

  2. #2
    garrettroyce's Avatar
    garrettroyce is offline Generally Helpful Member garrettroyce is a glorious beacon of lightgarrettroyce is a glorious beacon of light
    Join Date
    Apr 2008
    Location
    IL, USA
    Posts
    3,746

    Re: MySQL Export to csv

    Is this supposed to be a newline character?

    Code:
    $out .="n";
    gjr.gr - coming soon: secrets of OCD coding from a self taught tinkerer

  3. #3
    Livewire's Avatar
    Livewire is offline Abuse Compliance Officer Livewire is a glorious beacon of lightLivewire is a glorious beacon of light
    Join Date
    Jun 2005
    Location
    Behind a keyboard.
    Posts
    8,998

    Re: MySQL Export to csv

    Quote Originally Posted by garrettroyce View Post
    Is this supposed to be a newline character?

    Code:
    $out .="n";
    Assuming it is, \n = newline, n = just the letter, which may very well be the issue.
    Last edited by Livewire; 05-21-2009 at 06:47 PM.


    TOS breakers will be suspended regardless of race, creed, national origin, hair color, or favorite food. Thanks for your understanding!

  4. #4
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: MySQL Export to csv

    just a reminder, mysql_list_fields() is deprecated. Which means that it is now only kept for background compability, and for lazy coders. You should be better off switching to mysql_query("SHOW COLUMNS FROM sometable").
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  5. #5
    freecrm's Avatar
    freecrm is offline x10 Elder freecrm is an unknown quantity at this point
    Join Date
    May 2008
    Location
    UK
    Posts
    629

    Re: MySQL Export to csv

    Quote Originally Posted by garrettroyce View Post
    Is this supposed to be a newline character?

    Code:
    $out .="n";
    Oops!
    Edit:
    Quote Originally Posted by xav0989 View Post
    just a reminder, mysql_list_fields() is deprecated. Which means that it is now only kept for background compability, and for lazy coders. You should be better off switching to mysql_query("SHOW COLUMNS FROM sometable").
    I have to admit, SQL queries are not my forte so I wasn't aware of this, but I will try this later this evening.
    Last edited by freecrm; 05-22-2009 at 06:38 AM. Reason: Automerged Doublepost

  6. #6
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: MySQL Export to csv

    Quote Originally Posted by freecrm View Post
    I have to admit, SQL queries are not my forte so I wasn't aware of this, but I will try this later this evening.
    I found out about the deprecation when I looked it up. I've never used the mysql_list_fields functions. I always use SQL...
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  7. #7
    tomasonal is offline x10Hosting Member tomasonal is an unknown quantity at this point
    Join Date
    Feb 2011
    Posts
    5

    Smile Re: MySQL Export to csv

    Your issue looks interesting. There is a component specializes in large amount of data export, and many formats are supported, such as Excel,Word,PDF and so on. If you want to export data from database, you may choose it, it's very fast and stable when you export data from database. And now, it's free for everyone. You may visit here for further information: http://www.codeproject.com/KB/cs/Exc...23#xx3767223xx And if you have questions, you can send email to me, my email address is: tomlutao@gmail.com

Closed Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Which is better postgresql or MySQL?
    By Kurai Kumo in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 12-10-2009, 08:17 AM
  3. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  4. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers