user1020069
user1020069

Reputation: 1570

CSV file read fail (PHP )

I am trying to read a CSV file (delimited by commas) but unfortunately, it isn't responding as it ought to. I am not so sure what I am doing wrong here, but I'll paste out the contents of the code and the CSV file both :

$row = 0;
if($handle = fopen("SampleQuizData.csv","r") !== FALSE)
{
    // WORKS UNTIL HERE, SO FILE IS BEING READ
    while(!feof(handle))
    {
        $line = fgetcsv($handle, 1024, ",") ;
        echo $line[2];  // DOES NOT WORK
    }    
}

Here is the CSV file: (the emails and names have been changed here to protect the identities of the users)

parijat,something,[email protected]
matthew,durp, [email protected]
steve,vai,[email protected]
rajni,kanth,[email protected]

Upvotes: 0

Views: 303

Answers (3)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Give this a try:

<?php

$row = 0;

if (($handle = fopen("SampleQuizData.csv", "r")) !== FALSE)
{
    while(!feof($handle))
    {
        $line = fgetcsv($handle, 1024, ",") ;
        echo "$line[2]";
    }    
}

?>

Upvotes: 1

n0nag0n
n0nag0n

Reputation: 1668

It's worth a mention but when I was working on CSV exports a few weeks ago, I had weird line ending inconsistencies. So I put this at the top of my php file and it worked splendid.

<?php
    ini_set("auto_detect_line_endings", true);
?>

Upvotes: 0

Bibix
Bibix

Reputation: 345

it lacks a '$' to the handle variable

while(!feof($handle)){

and not :

while(!feof(handle)){

Upvotes: 2

Related Questions