1

I'd like to pass my PDO query parameters as an array to my execute() method, rather than using PDO's bindParams. I'm doing this so I can create one function to dynamically execute similar queries with differing numbers of parameters.

The bindParam() route yields the expected results:

$stmt->bindParam(":user_id",$user_id,PDO::PARAM_INT);
$stmt->bindParam(":n",$n,PDO::PARAM_INT);

$stmt->execute();

But this method returns an empty set with no errors thrown:

$params = array(":user_id"=>$user_id,":n"=>$n);

$stmt->execute($params);

Can someone explain to me what the difference between these two methods is, and what is causing my issue? The only difference I'm seeing is that when I pass $params as an argument to .execute, I'm not getting a chance to specify the type (POD::PARAM_INT). If that is the issue, is there a workaround for this?

sqlab
  • 6,412
  • 1
  • 14
  • 29
Nathan Wiles
  • 841
  • 10
  • 30
  • This is not a duplicate question. That question asks which one to use, mine asks how to properly use execute. Please read more carefully before marking a question as a duplicate. – Nathan Wiles Feb 10 '14 at 10:02
  • 2
    Your question is a typical [**xy problem**](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) If you had asked about binding LIMIT variable you would have found plenty of Answers [**ie**](http://stackoverflow.com/questions/2269840/php-pdo-bindvalue-in-limit) – david strachan Feb 10 '14 at 12:43
  • @davidstrachan, I agree. I try to be specific, but I didn't realize at the time of asking the question binding a value to LIMIT had anything to do with my problem. – Nathan Wiles Feb 10 '14 at 19:09
  • 1
    This question appears to be off-topic because it turned out to be an xy problem explored in comments ie LIMIT clause – david strachan Feb 23 '14 at 14:48
  • There I would disagree. PDO can pass integer parameters via the LIMIT clause IF you use bindParam and specify PDO::PARAM_INT, but as far as I know so far can't do so via execute. The question as such is totally on-topic. – Nathan Wiles Feb 24 '14 at 08:30

1 Answers1

2

I've discovered that this query failed because I was attempting to assign a paramater to a LIMIT clause value. When passing parameters to the execute function, PDO wraps ALL parameters in quotes, and the LIMIT clause can't handle quoted parameters.

The solution (until I find something better, which I'm still hoping for) is old-school string concatenation on the query to generate the complete LIMIT clause before adding the rest of my parameters.

Nathan Wiles
  • 841
  • 10
  • 30