0

I'm trying to get the right syntax for the following. In this case $post_pub = 1

$sql='SELECT "Publications"."Pub_ID", "Publications"."ART_TITEL" FROM "Publications" where "Pub_ID"="$post_pub"';

Php throws an error: column "$post_pub" does not exist

I've stumbled across pg_query_params, this feels like the right direction, but I need some help. How can I get this to work?

3

2 Answers 2

3

I never used pg_connect though I think you need something like this:

$sql='SELECT "Publications"."Pub_ID", "Publications"."ART_TITEL" 
FROM "Publications" 
where "Pub_ID"=$1 ';


$result = pg_query_params($dbconn, $sql, array($post_pub));
Sign up to request clarification or add additional context in comments.

3 Comments

Warning: pg_query_params(): Query failed: ERROR: syntax error at or near "$" LINE 1: ...ns"."ART_TITEL" FROM "Publications" where "Pub_ID"=$post_pub ^ in ...
var dump shows the query: string(103) "SELECT "Publications"."Pub_ID", "Publications"."ART_TITEL" FROM "Publications" where "Pub_ID"=$post_pub"
Ok, solved, I did change $Pub_ID to $1, I assume you can't repeat variables here.
0

the problem is double quotes around variable. Postgres understands it as "database object" name, in this part of query, a column. to avoid it, try using:

$sql='SELECT "Publications"."Pub_ID", "Publications"."ART_TITEL" FROM "Publications" where "Pub_ID"='."$post_pub";

also consider moving to PDO - such usage is a straight invitation for sql injection. Setting$post_pub to 0 or (delete from Publications)" will delete all data if user has enough right, for example.

5 Comments

after changing this error: Warning: pg_query(): Query failed: ERROR: syntax error at or near "$" LINE 1: ...ns"."ART_TITEL" FROM "Publications" where "Pub_ID"=$post_pub ^ in ...
Do I need to use pg_prepare and pg_execute? I'm a noob to postgresql after using mysql for a number of years.
@GaryNobles yes
Thank you for editing the code, I'm using pg_connect not PDO, the code isn't working, throws a http 500 error.
the @Oto's advice is preferable - use pg_query_params

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.