0

I have .csv file with 9k rows. When I use script from php manual :

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

I got on my output everysingle cell from the csv file.

Problem occurs when I want to treat every row as record create object from it and store it into database. I have modified it like:

if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
        $num = count($data);

        $record= new Record($db);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
                $record->$attributes[$c] =  utf8_encode($data[$c]);
        }
    $record->Store();
    }
    fclose($handle);
}

In this case only about 2k records are store into mine PostgreSQL database, but no exception or error is shown. I have no idea why my loop stops. Everytime i run the script it loads into mine database different amount of records between 1,6k and 2,1k.

I have no limits set on my PostgreSQL (At least i don't knew of any..)

Can anyone explain me what am I doing wrong?

2 Answers 2

1

Two Things here I will change A. change the !== FALSE) to !=false), don't ask me why , it sometimes work B. do NOT NEW you record instance in the loop without unset it , you are creating 2000 new instance so that may be the reason.

Sign up to request clarification or add additional context in comments.

Comments

0

You could try the copy statement. I belive it is similar to MySql LOAD DATA IN FILE

http://www.postgresql.org/docs/current/static/sql-copy.html

If it's just the data you need to add, it would be considerably faster that any other method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.