Nothing happening after the fgetcsv loop. Trying to save data from a tab delimitted report from amazon mws api

StackOverflow https://stackoverflow.com/questions/19014341

  •  29-06-2022
  •  | 
  •  

Question

while ($keys = fgetcsv($fp, 0, "\t")) {

        if ($c == 0) {
            $headers = $keys;

        } else {
            $rows[] = $keys;
            //var_dump($keys);
        }

         $c ++;
    }

fclose($fp);
echo count($rows);

If I echo the $count it works fine(shows the proper count), even if I dump the keys (commented line) it echoes as expected, but nothing happens after the loop ends. at around 9154 rows;

These lines below don't work and the script execution stops without any apparent error.

fclose($fp);
echo count($rows);
Was it helpful?

Solution

Try as in the PHP manual:

while( ( $keys = fgetcsv( $fp, 0, "\t" ) ) !== FALSE ) {

Also, make sure your input csv has a clean end of file.

OTHER TIPS

Try this and tell us what happens:

// make sure we see all errors
error_reporting(-1);
ini_set('display_errors', true);

// read the entire file into an array, one element per row
$rows = array();
while($fields = fgetcsv($fp, 0, "\t")) {
    $rows[] = $fields;
}
fclose($fp);

// handle empty CSV case
if(count($rows) == 0) {
    die('CSV is empty');
}

$headers = $rows[0];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top