0

I need to import a csv file into my database. I first wrote the function in SQL but now I need to do it in php, I don't want to change my entire code so I just figured I could execute the SQL query with php but I get an error.

    $query="COPY bet_import FROM $1 DELIMITER ',' CSV HEADER";
    $result = pg_prepare($dbh, "", $query);
    $result = pg_execute($dbh, "",array('bet.csv'));

I get the error:

Error: Warning: pg_prepare(): Query failed: ERROR: syntax error at or near "$1" LINE 1: COPY bet_import FROM $1 DELIMITER ',' CSV HEADER

0

1 Answer 1

0

You cannot bind identifiers (table name, field name) in the prepare statement. You need to add that into the query. If you are dynamically getting it you can escape it or sanitize it before you run the query. In your query the file path is an identifier.

$filePath = 'bet.csv';
$query="COPY bet_import FROM '$filePath' DELIMITER ',' CSV HEADER";
$result = pg_prepare($dbh, "", $query);
$result = pg_execute($dbh, "",array());

You have to pass the filename in the query itself. This query will run on the postgresql so you need to specify the absolute path of the file.. If this file is placed in same code as your php code then also you need to specify the absolute path.

Sign up to request clarification or add additional context in comments.

Comments

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.