Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-15-2013, 09:48 AM   PM User | #1
meshi
New Coder

 
Join Date: Aug 2007
Posts: 49
Thanks: 6
Thanked 1 Time in 1 Post
meshi is an unknown quantity at this point
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);
}
fclose($fp);

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

hope someone could help
meshi is offline   Reply With Quote
Old 01-15-2013, 12:21 PM   PM User | #2
Arcticwarrio
Regular Coder

 
Arcticwarrio's Avatar
 
Join Date: May 2012
Location: UK
Posts: 624
Thanks: 16
Thanked 70 Times in 70 Posts
Arcticwarrio is on a distinguished road
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.
Arcticwarrio is offline   Reply With Quote
Old 01-15-2013, 01:58 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,752
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:11 AM.


Advertisement
Log in to turn off these ads.