我正在嘗試使用預處理語句對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,並不能找出我做錯了。我是否需要安裝庫來使用它或什麼。謝謝!
這是我得到的錯誤:
跳轉出來......在'SELECT * FROM $table WHERE $column = $1'
使用雙引號
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
對於要評估的變量,您可能需要用雙引號引起您的查詢。 –