0

我正在尝试使用准备好的语句来使用 PHP 和 Postgres 进行简单的插入。到目前为止,我已经这样做了:

<?php
$conn_string = "host=localhost port=5432"; // plus us/pw
$dbconn = pg_connect($conn_string); 

$table  = 'business_primary_category';
$column = 'primary_category';
$tag = 'restuarant';


// Prepare a query for execution
$result = pg_prepare($dbconn, "my_query", 'SELECT * FROM $table WHERE $column = $1');

// Execute the prepared query.  Note that it is not necessary to escape
// the string "Joe's Widgets" in any way
$result = pg_execute($dbconn, "my_query", array("$tag"));

?>

我基本上是从 php.net 上的页面中获取的,并且无法弄清楚我做错了什么。我是否需要安装一个库才能使用它或其他东西。谢谢!

这些是我得到的错误:

Warning: pg_prepare() [function.pg-prepare]: Query failed: ERROR: syntax error at or near "$" at character 15 in /home/url    **......**   pdo.php on line 11

Warning: pg_execute() [function.pg-execute]: Query failed: ERROR: prepared statement "my_query" does not exist in /home/url    **......**   pdo.php on line 15

Warning: pg_execute() [function.pg-execute]: Query failed: ERROR: prepared statement "my_query" does not exist in /home/url    **......**   pdo.php on line 18
4

2 回答 2

3

如果要对变量进行插值,则需要使用双引号;否则 PHP 将其视为字符串文字。尝试:

$result = pg_prepare($dbconn, "my_query", "SELECT * FROM $table WHERE $column = $1");
于 2012-09-17T20:23:00.887 回答
2

跳出来的第一件事......在 . 上使用双引号'SELECT * FROM $table WHERE $column = $1'

于 2012-09-17T20:22:33.650 回答