Basti
Basti

Reputation: 15

Data incomplete -- Importing CSV-files into a PostgreSQL database using PHP

I have a problem importing csv-files into a PostgreSQL database using php. The csv-file holds around 11000 datalines, however php only imports around 2100 lines. (I looked through the data and found special characters like <!--. I replaced them with "nan" but the problem remains.)

When I import it with the pgAdmin wizard it works fine and imports the data completely (even with the special characters left in).

$filename=$_FILES["file"]["tmp_name"];      

     if($_FILES["file"]["size"] > 0)
     {
        ini_set("auto_detect_line_endings", true);
        $file = fopen($filename, "r");
        $flag = true;
        $row = 1;

        while (($getData = fgetcsv($file, 10000, ";")) !== FALSE)
         {
            if($flag) { $flag = false; continue; }

            $sql = "INSERT into public.fulldict (studiennr,studienname,formular,formularmodul,modul_id,titel_der_ableitung,abl_version,feldname,hilfetext,variablenname,pflichtfeld,feldtyp,feldlaenge,min_wert,max_wert,einheit,code,read_only,exeptional_values,passiv) 
                    values ('".pg_escape_string($getData[0])."','".pg_escape_string(($getData[1]))."','".pg_escape_string(($getData[2]))."','".pg_escape_string(($getData[3]))."','".pg_escape_string(($getData[4]))."','".pg_escape_string(($getData[5]))."','".pg_escape_string(($getData[6]))."','".pg_escape_string(($getData[7]))."','".pg_escape_string(($getData[8]))."','".pg_escape_string(($getData[9]))."','".pg_escape_string(($getData[10]))."','".pg_escape_string(($getData[11]))."','".pg_escape_string(($getData[12]))."','".pg_escape_string(($getData[13]))."','".pg_escape_string(($getData[14]))."','".pg_escape_string(($getData[15]))."','".pg_escape_string(($getData[16]))."','".pg_escape_string(($getData[17]))."','".pg_escape_string(($getData[18]))."','".pg_escape_string(($getData[19]))."')";
                $result = $con -> prepare($sql);
                $result -> execute();
            if(!isset($result))
            {
                echo "<script type=\"text/javascript\">
                        alert(\"Invalid File:Please Upload CSV File.\");
                        window.location = \"upload.php\"
                      </script>";   
            }
            else {
                echo "<script type=\"text/javascript\">
                    alert(\"CSV File has been successfully Imported.\");
                    window.location = \"upload.php\"
                </script>"; 
            }

         }
         fclose($file); 
         ini_set("auto_detect_line_endings", false);
     }

Upvotes: 0

Views: 585

Answers (1)

Basti
Basti

Reputation: 15

It works now. I had to move the if(!isset($result)){} outside of the loop. The check caused the loop to end prematurely. However I don't see how $resultcould be empty before the csv-file is read completely...

Upvotes: 0

Related Questions