使用PHP / PDO将.txt文件复制到Postgres中

时间:2013-05-10 20:07:06

标签: php sql postgresql pdo

我试图使用下面的代码将.txt文件输入到postgres数据库中。

当我使用pgAdminIII直接在数据库中输入时,这个sql查询有效但在php代码中我得到语法错误:

PHP Parse error:  syntax error, unexpected T_STRING in D:\Apapub\public_html\databases\B13\upload_files.php on line 34

当我从文本文件中删除('')时,如下所示:

$sql = 'COPY "public"."tblSedimentGrabEvent" FROM D:/Apapub/public_html/databases/B13/temp/grabevents.txt CSV';

我从sql端收到错误。 任何帮助将不胜感激,显然我是编码世界的新手。

由于

<?php
try { 
$db = new PDO("pgsql:dbname=NAME;host=HOST","USER","PASS");

}   
catch(PDOException $e) { echo $e->getMessage();
}

if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Uploaded Successfully!<br>";
echo "File Name: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"]. "<br>";

if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
    echo $_FILES["file"]["name"] . " already exists. ";
  }
    else
  {
    move_uploaded_file($_FILES["file"]["tmp_name"],
    "D:/Apapub/public_html/databases/B13/temp/" . $_FILES["file"]["name"]);
    echo "Stored in: " . "D:\Apapub\public_html\databases\B13\temp" . $_FILES["file"]["name"]. "<br>";
  }

 }

$sql = 'COPY "public"."tblSedimentGrabEvent" FROM 'D:/Apapub/public_html/databases/B13/temp/grabevents.txt' CSV';

$resultC = $db->query($sql); 
if (!$resultC){
echo ('The query did not succeed<BR>');
echo ($sql);

}   

?>

1 个答案:

答案 0 :(得分:1)

php方面的问题是你不正确地使用引号。尝试改变

$sql = 'COPY "public"."tblSedimentGrabEvent" FROM 'D:/Apapub/public_html/databases/B13/temp/grabevents.txt' CSV';
                                                  ^                                                       ^

$sql = 'COPY "public"."tblSedimentGrabEvent" FROM "D:/Apapub/public_html/databases/B13/temp/grabevents.txt" CSV';

$sql = 'COPY "public"."tblSedimentGrabEvent" FROM \'D:/Apapub/public_html/databases/B13/temp/grabevents.txt\' CSV';