[英]Data incomplete — Importing CSV-files into a PostgreSQL database using PHP
I have a problem importing csv-files into a PostgreSQL database using php. 我使用php将csv文件导入PostgreSQL数据库时遇到问题。 The csv-file holds around 11000 datalines, however php only imports around 2100 lines. csv文件可容纳约11000条数据线,但是php仅可导入2100行。 (I looked through the data and found special characters like <!--
. I replaced them with "nan" but the problem remains.) (我查看了数据,发现了<!--
类的特殊字符。我将其替换为“ nan”,但问题仍然存在。)
When I import it with the pgAdmin wizard it works fine and imports the data completely (even with the special characters left in). 当我使用pgAdmin向导导入它时,它可以正常工作并完全导入数据(即使保留了特殊字符)。
$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);
}
It works now. 现在可以使用了。 I had to move the if(!isset($result)){}
outside of the loop. 我不得不将if(!isset($result)){}
移到循环之外。 The check caused the loop to end prematurely. 检查导致循环过早结束。 However I don't see how $result
could be empty before the csv-file is read completely... 但是我看不到在完全读取csv文件之前$result
如何$result
空...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.