2

我试图使用下面的代码将 .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);

}   

?>
4

1 回答 1

2

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';
于 2013-05-10T20:20:54.617 回答