Jump to content

Recommended Posts

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.