JasonHarper Posted September 20, 2010 Share Posted September 20, 2010 Hello, I have a page where the user uploads a CSV file and I want to read that CSV and update database fields accordingly. It seems I have all the code correct but only the first row of the CSV is being imported. If I echo my variables within the loop, the only output is the first row of the CSV - no subsequent rows. I feel like I'm close but not sure what the issue is? Thanks a million for any help!! Jason //START FILE OPERATION $handle = fopen('uploads/testfile.csv', "r"); //LOOP CONTENTS OF CSV FILE while (($data = fgetcsv($handle, 1000, ",")) !== false) { $importID = $data[0]; $companyName = $data[1]; $primaryPhone = $data[2]; $primaryEmail = $data[3]; $billingFirstName = $data[4]; $billingLastName = $data[5]; $query = 'SELECT importID FROM import'; if (!$result = mysql_query($query)) { continue; } if ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { //UPDATE IF ENTRY EXISTS $query = "UPDATE import SET companyName='$companyName', primaryPhone='$primaryPhone', primaryEmail='$primaryEmail', billingFirstName='$billingFirstName' WHERE importID=$importID"; mysql_query($query); if (mysql_affected_rows() <= 0) { //NO RECORDS UPDATED } } else { //ENTRY DOESN'T EXIST } mysql_free_result($result); } //CLOSE FILE OPERATION fclose($handle); Link to comment https://forums.phpfreaks.com/topic/213925-fgetcsv-loop-issue/ Share on other sites More sharing options...
schilly Posted September 20, 2010 Share Posted September 20, 2010 echo out $data and see what it looks like. I had issue before with it not recognizing the line endings properly. Link to comment https://forums.phpfreaks.com/topic/213925-fgetcsv-loop-issue/#findComment-1113419 Share on other sites More sharing options...
JasonHarper Posted September 20, 2010 Author Share Posted September 20, 2010 Thanks for your help! OK - so that makes things even stranger. When I echo $data, I just get Array - it's completely empty. Not sure I understand why - I double checked the CSV file and the data is in there... Jason Link to comment https://forums.phpfreaks.com/topic/213925-fgetcsv-loop-issue/#findComment-1113422 Share on other sites More sharing options...
JasonHarper Posted September 20, 2010 Author Share Posted September 20, 2010 OK - disregard my last post - just needed to do a print_r instead of echo. I think I may know what the problem is I just don't know how to fix it. When I print the array, there aren't individual sets for each row. For example, the array is just [0]=>value, [1]=>value.....[255]=>value. Shouldn't there be some "nesting" for each line of the CSV? Thank you! Jason Link to comment https://forums.phpfreaks.com/topic/213925-fgetcsv-loop-issue/#findComment-1113424 Share on other sites More sharing options...
JasonHarper Posted September 20, 2010 Author Share Posted September 20, 2010 I just did a quick Google search for end of line issues with fgetcsv and one post said to add this: ini_set('auto_detect_line_endings', true); I added that to my code and it appears to be working perfectly! THank you schilly for putting me on the right track! Jason Link to comment https://forums.phpfreaks.com/topic/213925-fgetcsv-loop-issue/#findComment-1113426 Share on other sites More sharing options...
schilly Posted September 20, 2010 Share Posted September 20, 2010 np. yup that's the same fix I used. Link to comment https://forums.phpfreaks.com/topic/213925-fgetcsv-loop-issue/#findComment-1113428 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.