php - 来自 php 的 postgresql 查询 - pg_query_params() 期望参数 1 为资源,给定为空

标签 php postgresql

PHP 版本:7 Postgres 版本:9.6

我们正在开发一个向 postgres 数据库发送查询的 Web 应用程序,目前我正在更新一些易受 sql 注入(inject)攻击的查询。但是,当使用 pg_query_params 函数时,我收到错误

pg_query_params() 期望参数 1 为资源,给定为 null

这是指第二个参数,对吧?但是肯定不是null,是代表sql查询的字符串。这是我们的 PHP 代码(我知道 $FeatureCollection 仍在连接,因此存在 SQL 注入(inject)漏洞,但修复此代码涉及更多步骤):

$conn = dbconn();

$from = $_POST['from']; //node id of the 'from' location, passed from ajax
$to = $_POST['to']; //node id of the 'to' location, passed from ajax

$sql = 'SELECT json_build_object(
        \'type\',       \'FeatureCollection\',
        \'features\',   jsonb_agg(feature)
    )
    FROM (
        SELECT json_build_object(
            \'type\',       \'Feature\',
            \'id\',         edge,
            \'geometry\',   ST_AsGeoJSON(the_geom)::json,
            \'properties\', to_jsonb(row) - \'id\' - \'geom\'
    ) AS
        feature
    FROM
        (
            SELECT * FROM pgr_nogo_dijkstra(
                    \'SELECT gid AS id, source, target, cost, reverse_cost, the_geom AS geom FROM ways\',
                    (
                        SELECT
                            ST_SetSRID(ST_Union(ST_GeomFromGeoJSON(feat->>\'geometry\')), 4326)
                        FROM (
                            SELECT json_array_elements(
                                \'{
                                    "type": "FeatureCollection",
                                    "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
                                    "features":
                                        ['.$FeatureCollection.'] // still vulnerable, will be fixed later
                                }\'
                                ::json->\'features\'
                            ) AS
                                feat
                        ) AS
                            f
                    ),
                    $1,
                    $2,
                    TRUE
                ) AS route_result
            INNER JOIN
                ways
            ON
                ways.gid = route_result.edge
        )
    row) features;
    ';

result = pg_query_params( $conn, $sql, array($from, $to) ) or die('Query Failed: ' .pg_last_error());

$sql 绝对不是空的,它是预先定义好的。当我们只使用字符串连接和 pg_query() 时,查询是事先有效的,所以我不确定是什么导致 pg_query_params() 失败。

编辑:我也不应该认为 dbconn() 指的是从另一个 php 文件导入的另一个函数:

function dbconn(){

        ini_set('display_errors', 1); // Dispalys errors
        // fetch config params
        $config = file_get_contents('../config.json');
        $json = json_decode($config, true);
        //echo $json['db_host'];


        //database login info
        $host = $json['db_host'];
        $port = $json['db_port'];
        $dbname = $json['db_name'];
        $user = $json['db_user'];
        $password = $json['db_pwd'];
        // establish connection         
        $conn = pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");
        if (!$conn) {
            echo "Not connected : " . pg_error();
            exit;
        }
    }

最佳答案

好的:没有参数 1 不是第二个参数,而是第一个参数。 您的 dbconn 函数未返回连接。

添加一个

return $conn;

在你的 dbconn 函数的末尾,你应该没问题。

关于php - 来自 php 的 postgresql 查询 - pg_query_params() 期望参数 1 为资源,给定为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185069/

相关文章:

javascript - 当其他 AJAX 函数已执行时,第二个 AJAX 函数失败

php - filter_input(INPUT_GET, ‘my_string’, FILTER_SANITIZE_STRING); VS 正则表达式预匹配 PHP

PostgreSQL - 使用 SUM 意外除以零

postgresql - 使用 Postgres,我想要查询中字符串中特定格式的时间戳

postgresql - 使数据库中的所有表都未登录

php - 在 python 中使用 struct 模块在 pack() 中选择格式

php - 登录系统在数据库中存储 session

postgresql - 如何获取 PostgreSQL 中的行策略列表?

postgresql - 更改现有表的填充因子

php - Laravel4 : upload a photo