Connecting Tech Pros Worldwide Help | Site Map

fgetcsv() doesn't get last row in file

Newbie
 
Join Date: Mar 2008
Posts: 2
#1: Mar 19 '08
I have a form set up to allow someone to upload a .csv and populate its data into a database using the fgetcsv() function. All of the code works fine because everything in the .csv file is populated into the database just fine, except for the very last row of data. I have tried several different ways of using the fgetcsv(), including:
[php]
while ($result = fgetcsv($handle)) {

while (($result = fgetcsv($handle)) != false) {

while (($result = fgetcsv($handle)) !== false) {
[/php]
I have tried each one of these also using the optional $length, $delimiter, and $enclosure parameters, but to no avail.

I am using the function exactly (as far as I can tell) as it is shown on php.net, but cannot figure out why the last row of my file would not be included in the database... any suggestions? Server is running PHP 5.2.5
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#2: Mar 19 '08

re: fgetcsv() doesn't get last row in file


A known error is that "If the last char of the CSV file isn't a carriage return, the function can't take the last field of the last line in the CSV.".

Is this what happened? If not, show some more code of your script. Maybe it is somwhere else. (and show any code within appropriate code tags).

Ronald
Newbie
 
Join Date: Mar 2008
Posts: 2
#3: Mar 19 '08

re: fgetcsv() doesn't get last row in file


Thanks for the tip... that got me into looking at the code that inserted the data in the database. It turns out I hadn't accounted for single quotes in some of the fields that was breaking up my arguments. I actually had about 10 rows not being entered into the database.
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#4: Mar 19 '08

re: fgetcsv() doesn't get last row in file


Does that mean that your original problem, why you started the thread, is now solved?

Ronald
Reply