php - Stop insert of first row from CSV

66

Im currently importing from a CSV and i cant seem to figure out how to stop the insert of the first row.

if ($_FILES[csv][size] > 0) {
//get the csv file 
$file = $_FILES[csv][tmp_name]; 
$handle = fopen($file,"r");     
//loop through the csv file and insert into database 

do { 
    if ($data[0]) { 
        mysql_query("INSERT INTO TABLE (id,type) VALUES 
        (
        '".addslashes($data[0])."', 
        '".addslashes($data[1])."'
        )");
        } 
} 
while ($data = fgetcsv($handle,0,",",'"'));

echo "Done";
}

Any help would be awesome!

744

Answer

Solution:

If all you are trying to do is skip the first then drop thedo and turn your loop into a regularwhile and make sure to callfgetcsv() once before the loop.

if ($_FILES[csv][size] > 0) {
    //get the csv file 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r");     
    //loop through the csv file and insert into database 

    //Get some data first, and do nothing with it.
    $data = fgetcsv($handle,0,",",'"');

    while ($data = fgetcsv($handle,0,",",'"')) { 
        if ($data[0]) { 
            mysql_query("INSERT INTO TABLE (id,type) VALUES 
            (
            '".addslashes($data[0])."', 
            '".addslashes($data[1])."'
            )");
        } 
    } 

    echo "Done";
}
161

Answer

Solution:

Before looping callfgetcsv once -

if ($_FILES[csv][size] > 0) {
    //get the csv file 
    $file = $_FILES[csv][tmp_name]; 
    $handle = fopen($file,"r");
    $data = fgetcsv($handle); // this calls the first row     
    //loop through the csv file and insert into database 

People are also looking for solutions to the problem: exchangewebservices - Creating a recurring calendar event with php-ews

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.