pg_query_params is not substituting parameters in the query

666 Views Asked by At

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

There are 0 best solutions below