[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


fgetcsv loop returns 1 row to many

Posted on 2004-11-02
Medium Priority
Last Modified: 2008-03-06
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 ;)
Question by:bennybutler
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 12474882

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?

Accepted Solution

BenMorel earned 500 total points
ID: 12476156
No, you're right to use !== false, because fgetcsv() can also return a NULL value for an empty line, and I think you don't want your script to stop in this case, but to skip this empty line, right ?
Try this code :

while (($data = fgetcsv($csv, filesize("./trial_imports/".$file), ",")) !== false) { // note: it's while() and not do() ;)
 if (is_null($data)) continue; // skip if line is empty
 //perform actions

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question