数据不完整 -- 使用 PHP 将 CSV 文件导入 PostgreSQL 数据库
Data incomplete -- Importing CSV-files into a PostgreSQL database using PHP
我在使用 php 将 csv 文件导入 PostgreSQL 数据库时遇到问题。 csv 文件包含大约 11000 条数据线,但是 php 仅导入大约 2100 条数据线。 (我查看了数据并发现了特殊字符,例如
<!--
。我用 "nan" 替换了它们,但问题仍然存在。)
当我使用 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);
}
现在可以了。我不得不将 if(!isset($result)){}
移出循环。检查导致循环提前结束。但是,在完全读取 csv 文件之前,我不明白 $result
怎么会是空的...
我在使用 php 将 csv 文件导入 PostgreSQL 数据库时遇到问题。 csv 文件包含大约 11000 条数据线,但是 php 仅导入大约 2100 条数据线。 (我查看了数据并发现了特殊字符,例如
<!--
。我用 "nan" 替换了它们,但问题仍然存在。)
当我使用 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);
}
现在可以了。我不得不将 if(!isset($result)){}
移出循环。检查导致循环提前结束。但是,在完全读取 csv 文件之前,我不明白 $result
怎么会是空的...