TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-15-2008, 09:08 PM   #1 (permalink)
The Contributor
 
webosb's Avatar
 
Join Date: Nov 2007
Posts: 41
Thanks: 24
webosb is on a distinguished road
Default exporting data to csv

currently i have a script that exports to csv file from mysql but it seems as there are more rows of data, the script kind of stops working...

current i'm using this function:

PHP Code:
function generatecsv($start$end){
$delim ",";
$query "SELECT * FROM users WHERE userid >= '$start' AND userid <= '$end'";

$d db();
$result mysql_query($query$d);
mysql_close($d);


$csv_output "userid,email,firstname,lastname";
$csv_output .= "\n";

while(
$row mysql_fetch_array($result)) { 
$uid $row['userid'];
$em $row['username'];
$fn $row['firstname'];
$ln $row['lastname'];

$csv_output .= $uid $delim;
$csv_output .= $em $delim;
$csv_output .= $fn $delim;
$csv_output .= $ln $delim;
$csv_output .= "\n";
}


header("Content-Type: application/force-download\n");
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header("Content-Disposition: attachment; filename=data_" date("dmY") . ".csv");

print 
$csv_output;
exit;

is there a better way of doing this when there's like 50,000 rows of data?
__________________
"Things you can get access to, you should never memorize." -Albert Einstein
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
webosb is offline  
Reply With Quote
Old 01-15-2008, 09:11 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 217
Thanks: 16
RobertK is on a distinguished road
Default

Firstly, there's a time limit on execution. Maybe one of the others remember what to set to a long time period.

Also, you should do an unbuffered query, which will speed things up. This way you just grab results until a null/false is returned and echo out the row. It'll be a good bit faster with 50k rows.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 01-15-2008, 09:15 PM   #3 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

If you're using PHP 5.1+, then you might find the function fputcsv() useful for generating csv files =)
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design