6

Can't PDO bind a value to multiple occurrences of a param in a query with a single bindParam()?

I'm surprised, I thought it was possible, but I didn't find any info on php's docs on this, neither on the web. Any clarification / alternative is welcome!

Note : I'm using php 5.3.6 / 5.3.8 (dev/prod)


Example :

Consider this prepared statement :

INSERT INTO table VALUES (:param1, 0), (:param1, 1);

Now, if I bind values to my query:

bindParam(":param1",$my_param1);

I have a PDO error :

SQLSTATE[HY093]: Invalid parameter number

Community
  • 1
  • 1
Maen
  • 10,603
  • 3
  • 45
  • 71

1 Answers1

6

See PDO::prepare

You cannot use a named parameter marker of the same name twice in a prepared statement
Manu
  • 922
  • 6
  • 16
  • Correct. It's not supported, it's documented and it's probably not going to change. But neither PDO itself nor the drivers seem to enforce the restriction: you'll find that it *works* with some driver/settings and fails with other. – Álvaro González Feb 14 '14 at 12:48
  • Ok, I wasn't looking at the right place... Too bad it's like that. Any idea of alternative binding with PDO, which could allow multiple binds in one shot, without named parameters? – Maen Feb 14 '14 at 12:49