查询中的PHP PostgresQL变量



我正在尝试获取以下内容的正确语法。在这种情况下,$ post_pub = 1

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

php引发错误:列" $ post_pub"不存在

我偶然发现了PG_QUERY_PARAMS,这感觉就像是正确的方向,但是我需要一些帮助。我该如何工作?

我从未使用过pg_connect,尽管我认为您需要这样的东西:

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

$result = pg_query_params($dbconn, $sql, array($post_pub));

问题是围绕变量的双引号。Postgres将其理解为"数据库对象"名称,在查询的这一部分,列。为避免它,请尝试使用:

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

还考虑搬到PDO-这种使用是对SQL注入的直接邀请。将$post_pub设置为0 or (delete from Publications)",如果用户有足够的正确性,将删除所有数据。

最新更新