我想对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..";

?>

我得到“查询未执行…”;
查询的字符串是使用javascript从html页面获取的。
在Apache2的error.log中,我得到:
PHP警告:pg_query():查询失败:错误:在“Penn”处或附近出现语法错误\n第1行:SELECT(addy).stateabrev FROM geocode(O
这里的重点是什么?

最佳答案

清除用户提供的数据:

$input = pg_escape_literal($conn, $input);
$sql = "SELECT (addy).stateabbrev FROM geocode($input);";

关于postgresql - PostgreSQL查询有效,但不适用于php,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14415529/

10-11 04:45
查看更多