Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: fputcsv slow

  1. #1
    New Coder
    Join Date
    Aug 2007
    Thanked 1 Time in 1 Post

    fputcsv slow

    Hi All,

    Good day.

    I created a script that put all data from mysql database to csv file. Im using fputcsv. It's working fine but when the data is large like 15000 and up the writing is verly slow. it took like 1-2 days to write it all in the csv.

    How would I do it fastly?

    This is my sample code and the it returns like 15000 records and more..

    $query = select * from table;
    $row = mysql_query($query);
    foreach ($rows as $row)

    $list = array($row['a'],$row['b'],$row['c'],$row['d']);

    $fp = fopen('file.csv', 'w');

    foreach ($list as $fields) {
    fputcsv($fp, $fields);

    I tried to execute the query on the database and it only took less than a minute.

    hope someone could help

  2. #2
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Thanked 100 Times in 100 Posts
    you could use php myadmin and export the data you want to a csv file
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  3. #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    Exporting is IMO the best solution as well. No point in reinventing the wheel on this.
    You have other serious problems if this is taking you like 1 - 2 days. This code's job is to write a single line into the CSV file, and what will be the last record of the query. Even if you have only 1 byte of memory left, I can't see 15K records taking 2 days to iterate through. If you only want the last record appended, than simply query with a LIMIT 1 and order in reverse.
    Or, you could post the code you actually have in use.


Posting Permissions

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