2

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);

}   

?>
2
  • What's the SQL error? Commented May 10, 2013 at 20:08
  • ERROR: syntax error at or near "D" LINE 3: COPY "tblSedimentGrabEvent" FROM D:/Apapub/public_html/datab... ^ Commented May 10, 2013 at 20:10

1 Answer 1

2

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';
Sign up to request clarification or add additional context in comments.

3 Comments

ERROR: syntax error at or near ""D:/Apapub/public_html/databases/B13/temp/grabevents.txt"" LINE 3: COPY "tblSedimentGrabEvent" FROM "D:/Apapub/public_html/data... ^
Thank You. that last line worked, can you explain why the forward and back slash worked for future reference?
@AbelSantana You're welcome. Since your string is single quoted in order to have a literal single quote in your $sql variable you need to escape it with a backslash (). Thus two \'s around your file path. Read more here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.