我想对PostgreSQL数据库进行php查询。我在服务器中测试了查询并返回,但是当我在php中尝试它时:
<?php
//Check the input
if (!isset($_POST['variable']))
echo "Address not selected";
$input = $_POST['variable'];
//$input = ucfirst(trim($_POST['variable']));
//echo $input;
$conn = pg_connect("host=localhost port=5432 dbname=geocoder user=postgres");
if (!$conn)
echo "Could not connect to server..";
$sql = "SELECT (addy).stateabbrev FROM geocode($input);";
$result = pg_query($conn, $sql);
if (!$result)
echo "Query did not executed..";
?>
我知道“查询没有执行..”;
QUERY的字符串是使用javascript从html页面获取的。
在Apache2的error.log中我得到:
PHP警告:pg_query():查询失败:错误:语法错误在“Penn”或附近\ nLINE 1:SELECT(addy).stateabbrev FROM geocode(O
这里有什么意义?
答案 0 :(得分:0)
清理用户提供的数据:
$input = pg_escape_literal($conn, $input);
$sql = "SELECT (addy).stateabbrev FROM geocode($input);";
答案 1 :(得分:0)
我设法找到了问题,即geocode()函数的$ input变量必须用单引号括起来:
geocode('$input')
:)