duanpa5237 2013-05-10 20:07
浏览 37
已采纳

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

Im trying to enter a .txt file into a postgres database using the code below.

This sql query works when I enter it directly in the database using pgAdminIII but in the php code i get a syntax error:

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

When I remove the ('') from around the text file like so:

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

I get a error from the sql side. any help will be appreciated, obviously I am new to the coding world.

Thanks

<?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条回答 默认 最新

  • douciping4283 2013-05-10 20:20
    关注

    The problem on php side is that you improperly use quotes. Try to change

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

    to

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

    or to

    $sql = 'COPY "public"."tblSedimentGrabEvent" FROM \'D:/Apapub/public_html/databases/B13/temp/grabevents.txt\' CSV';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历