gpt4 book ai didi

PostgreSQL 查询有效但不适用于 php

转载 作者:行者123 更新时间:2023-11-29 14:01:28 25 4
gpt4 key购买 nike

我想对 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 Warning: pg_query(): Query failed: ERROR: syntax error at or near "Penn"\nLINE 1: SELECT (addy).stateabbrev FROM geocode(O

这里有什么意义?

最佳答案

清理用户提供的数据:

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com