0

I'm pulling a CSV file into a script, parsing it and then updating my MySQL table. The problem is that it's only inserting the data from the second row of the CSV file (first row is being skipped because it's the headers of the columns). Here's my code:

$f = fopen("sample.csv", "r");
while(($line = fgetcsv($f)) !== FALSE){
    $time = $line[0];

    //SKIP FIRST LINE (WITH HEADERS)
    if($time == "Time"){
        continue;
    }
    else{
        $size = $line[1];
        $location = $line[2];
        $county = $line[3];
        $state = $line[4];
        $lat = $line[5];
        $lon = $line[6];
        $comments = $line[7];
        $sql = "INSERT INTO sample (time, size, location, county, state, lat, lon, comments) VALUES ('$time', '$size', '$location', '$county', '$state', '$lat', '$lon', '$comments')";
        $result = $mysqli->query($sql);
        //CHECK TO SEE IF THE LOOP IS WORKING THROUGH ALL DATA
        echo $size;
    }
}
fclose($f);

The issue is that it skips the first row successfully and then on the second loop through, it successfully updates the database. As you can see, I've echo'ed out the value of $size and it echoes all the values of all the rows meaning it's looping through all the data correctly. I'm not sure why it's not adding that data into each subsequent row.

Thanks for your help!

MillerMedia
  • 3,651
  • 17
  • 71
  • 150
  • 1
    You don't even check the success of your sql statement. So you won't see what mysql reports. – arkascha Jun 10 '13 at 08:10
  • 1
    hard to say without a content sample of your csv file – Petros Mastrantonas Jun 10 '13 at 08:12
  • I don't like the idea of using Header value to check, rather, I would either count or use a flag, if flag is false insert into db, otherwise not, and set it to true before loop and set to false within loop. Header might get change, why limit your imagination. – Sumit Gupta Jun 10 '13 at 08:16
  • @arkascha, I was just checking the database in phpMyAdmin when I ran the script. It showed the first line in the table successfully but that's all... – MillerMedia Jun 10 '13 at 17:36
  • Yeah, nice, but that is what you already knew. I asked why you don't check the success if the sql statement. That means instead of running a statement and turning around for the next statement you should check if mysql returned an error after the last statement you ran. There are means for that: http://php.net/manual/de/function.mysql-error.php. Also it certainly makes sense to dumo the whole statements you run, since apparently they are buggy. You make yourself a hard time by _not_ looking into what is actually happening. Instead you just look at the final result. – arkascha Jun 11 '13 at 08:05

1 Answers1

0

you need to separate the text from the file, try this:

while(($line = fgetcsv($f,4096,",", '"')) !== FALSE){
kraysak
  • 1,746
  • 1
  • 13
  • 14
  • What do you mean exactly? I'm successfully printing out all values of the table when I run the current script so I believe it is pulling the text out correctly? – MillerMedia Jun 10 '13 at 17:37