1

I'm probably missing something simple here. I'm trying to dynamically populate bindValue statements. From what I can tell they work up until they reach a set of code that pulls in key and value pairs from a dynamically generated set of facets.

The array for the bind values ($pdokeys) looks like this just before executing:

Array ( [:icapabilites1] => Bridges [:icapabilites2] => Transit and Rail [:icapabilites3] => Roads )

The code for bindValues looks like this:

try {
//$dbh is an existing pdo connection

$stmt2 = $dbh->prepare($sql);

foreach($pdokeys as $key => $value){
    if(is_numeric($value)){ $param = PDO::PARAM_INT; }
            elseif(is_bool($value))   { $param = PDO::PARAM_BOOL; }
            elseif(is_null($value))   { $param = PDO::PARAM_NULL; }
            elseif(is_string($value)) { $param = PDO::PARAM_STR; }
            else { $param = FALSE;}

echo "bindValue($key, $value, $param)<br/>";

if($param){$stmt2->bindValue($key, $value, $param);}


}

$stmt2->execute();
//if results do stuff here...
} catch (PDOException $search) {
echo $sql."<br/><br/>";
echo 'Connection failed line for search2: ' . $search2->getMessage();
}

Based on the writing out the pdo bindvalue just before binding the values, it appears the bindValue fails when I try to bind the first facet :icapabilities1.

The output looks like this.

bindValue(:icapabilites1, Bridges, 2)


SELECT * FROM careers WHERE scrapetime='1234567' AND ( capabilities= :icapabilities1 OR capabilities= :icapabilities2 OR capabilities= :icapabilities3 ) AND external_posted='Posted' ORDER BY last_updated desc;

Connection failed line for search2: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined 

The facets are added to the $pdokeys array that I'm using to create the PDO bindArray in the following fashion:

foreach($capabilities as $key => $value){
$thiskey = ":icapabilites".(intval($key)+1);
$pdokeys[$thiskey] = $capabilities[$key];
}

If I have other array elements in $pdokeys in front of the icapabilities1, those bindValues appear to work, (I get all the echo statements up till the bindVal icapabilities1), so I'm assuming it has to be something with the way I'm creating or passing the capabilities bindValue statement.

4

0 回答 0