joeshanley Posted September 1, 2010 Share Posted September 1, 2010 Hi, I am updating a few parts of my current website. Currently, the website updates user accounts by uploading CSV data reports. I currently use fgetcsv to deal with each row and this is taking forever to deal with the data as this is currently the code for each row. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $grabinfo = "SELECT * FROM transactions WHERE id = '$data[$i]'"; $grabinforesult = mysql_query($grabinfo) or die(mysql_error()); while($row = mysql_fetch_array($grabinforesult)){ $merchant = $row['merchant']; $username = $row['username']; $datetime = $row['datetime']; $letthem = $row['letthem']; $validate = "SELECT * FROM transactions WHERE merchant = '$merchant' and username = '$username' and (status='pending' or status='success' or status='confirmed') and datetime='$datetime'"; $validator = mysql_query($validate) or die(mysql_error()); if(mysql_num_rows($validator) >= $letthem) { $import="UPDATE transactions SET amount='0.00' WHERE id='$data[$i]' and status ='awaiting'"; mysql_query($import) or die(mysql_error()); }} mysql_query($import) or die(mysql_error()); } fclose($handle); This is performing lots of checks on the csv data and checking it with the system. It checks each row. Is there a quicker way of reading the csv data? There are lots of checks and while loops within the fgetcsv while loop. It can take upto half an hour to process a file with 1000 csv rows. Hope someone could offer some advise. Thanks Link to comment https://forums.phpfreaks.com/topic/212312-working-with-csv-file-data/ Share on other sites More sharing options...
joeshanley Posted September 2, 2010 Author Share Posted September 2, 2010 I've just created this. I think this will be quicker to store and then read the CSV data. function load_csv($file,$identifier,$commission,$purchase_value) { $lines = file($file) or die('Could not open file'); foreach($lines as $line) { $row_data = explode(",", $line); $new_row[0] = $row_data[$identifier]; $new_row[1] = $row_data[$commission]; $new_row[2] = $row_data[$purchase_value]; $transaction_data[] = $new_row; } return $transaction_data; } Is there any better way than this? Or can this code be easily improved? Help appreciated Thanks. Link to comment https://forums.phpfreaks.com/topic/212312-working-with-csv-file-data/#findComment-1106262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.