Nate
Nate

Reputation: 28384

Use bound variables more than once?

I have lots of queries where a variable that is bound needs to be used more than once. Here's a simple example.

$stmt = $db->prepare('SELECT SUM(col1),

                      (SELECT SUM(col2)
                      FROM table2
                      WHERE col3 > :val) as quantity

                      FROM table1
                      WHERE col4 = :val');

When I do something like this, I always get the error:

Error!: SQLSTATE[HY093]: Invalid parameter number in ...

There are typically reasons why I can't do something like

WHERE col3 = col4

In other words, there are situations where I simply need to use a bound variable more than once. In the past, I've just bound the values multiple times with slightly different names.

Is it possible to use a bound variable more than once?

Upvotes: 1

Views: 168

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157896

Either set PDO::ATTR_EMULATE_PREPARES to TRUE or use your "slightly different names" approach. It doesn't make too much difference though

Upvotes: 2

Related Questions