duanpu1064 2019-01-11 20:08
浏览 41

pg_query_params不替换查询中的参数

I want my application to dynamically change which database and schema name it uses depending on whether it's running on a local machine, staging environment, or production. I thought pg_query_params would be a great choice because you can dynamically substitute things into your queries by parameterizing them. The idea is the schema name will be dynamically inserted into the query. But, pg_query_params() doesn't seem to be substituting the values in the array, causing a query syntax error.

I have verified that the query itself is valid by using pg_query and hardcoding the schema name in versus parameterizing it. When I look at the logs, I see the following error:

PHP Warning: pg_query_params(): Query failed: ERROR: syntax error at or near "$1. Line 2 FROM $1.module__c

So, clearly, pg_query_params isn't actually substituting the parameters in (at least from what I can see)

As a result, pg_query_params() returns false, and breaks the whole page.

The code first gets an array from my databaseInfo() function (it returns an array for the schema name and connection resource. I've verified that this part of my code is working and returning the expected information.

$database = databaseInfo();
if (isset($projSFID)) {
    $placeholder = $projSFID;
    $query = "SELECT $1.module__c.name, $1.module__c.module_title__c, $1.module__c.number_of_units__c, $1.module__c.duration_minutes__c, $1.module__c.module_bg_hex__c, $1.module__c.module_description__c, $1.lms_asset__c.lms_asset_url__c
                FROM $1.module__c
                INNER JOIN $1.teproject__c ON $1.module__c.associated_project__c = $1.teproject__c.sfid
                INNER JOIN $1.lms_asset__c ON $1.module__c.module_image_url__c = $1.lms_asset__c.sfid
                WHERE $1.teproject__c.sfid = $2 AND $1.module__c.published__c=TRUE";
} elseif (isset($trackSFID)) {
    $placeholder = $trackSFID;
    $query = "SELECT $1.module__c.name, $1.module__c.module_title__c, $1.module__c.number_of_units__c, $1.module__c.duration_minutes__c, $1.module__c.module_bg_hex__c, $1.module__c.module_description__c, $1.lms_asset__c.lms_asset_url__c
                FROM $1.module_track_association__c
                    INNER JOIN $1.module__c ON $1.module_track_association__c.module__c = $1.module__c.sfid
                    INNER JOIN $1.track__c ON $1.module_track_association__c.track__c = $1.track__c.sfid
                    INNER JOIN $1.lms_asset__c ON $1.module__c.module_image_url__c = $1.lms_asset__c.sfid
                WHERE $1.track__c.sfid = $2
                ORDER BY $1.module_track_association__c.navigation_sequence__c";
} else {
    $placeholder = '';
    $query = "SELECT $1.module__c.name, $1.module__c.module_title__c, $1.module__c.number_of_units__c, $1.module__c.duration_minutes__c, $1.module__c.module_bg_hex__c, $1.module__c.module_description__c, $1.lms_asset__c.lms_asset_url__c
                FROM $1.module__c
                INNER JOIN $1.lms_asset__c ON $1.module__c.module_image_url__c = $1.lms_asset__c.sfid
                WHERE $1.module__c.published__c=TRUE AND $1.module__c.display_on_frontend_filtered_only__c=FALSE $2";
    echo $query;
}

$result = pg_query_params($database['connection'], $query, array($database['schema'],$placeholder));    

The expected result is that $1 will be the name of the schema and $2 will be the value of the placeholder as determined by the conditional logic.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于这个复杂问题的解释说明
    • ¥50 三种调度算法报错 采用的你的方案
    • ¥15 关于#python#的问题,请各位专家解答!
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败