Asheeown Posted April 5, 2007 Share Posted April 5, 2007 I need a script that makes a CSV file, but the results being loaded into the CSV files are by the millions in numbers, so I just need an efficient script. Can anyone help me get started on this? Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/ Share on other sites More sharing options...
kenrbnsn Posted April 5, 2007 Share Posted April 5, 2007 If you want to hire some one to do the job please post in the Freelancers area. This area is to help you with code you've already written or are attempting to write. Have you written any code yet? Ken Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/#findComment-222299 Share on other sites More sharing options...
Asheeown Posted April 5, 2007 Author Share Posted April 5, 2007 I have, it was basic csv file php commands and then it GZzips it but I was looking for something more efficient, meaning a hint towards such a thing. Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/#findComment-222304 Share on other sites More sharing options...
kenrbnsn Posted April 5, 2007 Share Posted April 5, 2007 How are you doing it now. Without seeing your current code, we probably can't begin to help you. Ken Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/#findComment-222315 Share on other sites More sharing options...
Asheeown Posted April 5, 2007 Author Share Posted April 5, 2007 <?php $fh = fopen('include/temp/temp.csv', "w") or die("PROBLEM!"); // Create a header row in the file fwrite($fh, "Source,Called,Destination,Ani,Call Time\n") or die("WE GOT A PROBLEM WRITING!"); // Loop through the results, writing to the file while ($row = mysql_fetch_assoc($UserRated1)){ $data = $row['Originating_TG'] . "," . $row['Inpulsed_Digits'] . "," . $row['IB_Region_Name'] . "," . $row['ANI'] . "," . $row['UTCTime'] . "\n"; fwrite($fh, $data); } // Close the file fclose($fh); // Compress a file compress("include/temp/temp.csv", "include/temp/temp.csv.gz"); echo "<a href=\"include/temp/temp.csv.gz\">Download link </a><br />"; ?> All the links are just temporary till I get a finished product and I can get a temp file script. Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/#findComment-222325 Share on other sites More sharing options...
kenrbnsn Posted April 5, 2007 Share Posted April 5, 2007 What does the query look like that you're using to pull the data from the database? If you are just pulling the fields you are writing, you can using implode() to put the commas between the fields. Also storing all the data in temporary array and then doing one write might be faster: <?php $data = array(); while ($row = mysql_fetch_array($UserRated1)){ $data[] = implode(',',$row); } fwrite($fh, implode("\n",$data); ?> I don't know if it's more efficient or not, but you're doing only one write instead of many. Ken Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/#findComment-222343 Share on other sites More sharing options...
Barand Posted April 5, 2007 Share Posted April 5, 2007 There is this option while ($row = mysql_fetch_assoc($UserRated1)){ fputcsv($fh, $row); } But fastest way is probably to bypass php and use MySQL "SELECT ... INTO OUTFILE filename FROM ...." Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/#findComment-222353 Share on other sites More sharing options...
Asheeown Posted April 5, 2007 Author Share Posted April 5, 2007 The MySQL command is perfect, thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/45767-solved-csv-effiecient-file-creation/#findComment-222365 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.