Link to home
Start Free TrialLog in
Avatar of bennybutler
bennybutler

asked on

fgetcsv loop returns 1 row to many

I have been using this loop that I pulled from the php.net website:

do (($data = fgetcsv($csv, filesize("./trial_imports/".$file), ",")) !== FALSE) {

//perform actions
}

But each time it runs, it always runs through one row too many, inserting a blank row into the database.

I tried making it a do/while but got the same results.

I would change it to loop through based on a count I keep up with, but I don't see a countrows function for the csv file. I've been up working on this for a long time, so I'm probably missing something simple.  Maybe someone can think about it while I sleep ;)
Avatar of rweston
rweston
Flag of United States of America image

Try:

while($data = fgetcsv($csv, filesize("./trial_imports/".$file), ",")) {

//perform actions
}


because the manual says fgetscsv returns false on error or EOF so maybe the !== FALSE doesnt match up with the EOF mark properly?
ASKER CERTIFIED SOLUTION
Avatar of BenMorel
BenMorel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial