views:

45

answers:

3

All,

I don't really understand the usage case for named placeholders bindParam(':blabla', $blabla) versus numbered placeholders bindParam(1, $blabla). Is it mostly a question of readability?

Thanks,

JDelage

+2  A: 

It's mostly just a readability thing. Personally, I would used named placeholders when possible. I would generally use numbered placeholders only if I was building a dynamic query where you don't know exactly what or how many parameters there will be until runtime.

Eric Petroelje
A: 

Simple Answers:

  • They allow the query to be prepared which means that if you have to execute that query multiple times, the SQL engine will not have to check that the syntax is correct
  • helps prevents SQL injection
  • improves readability
MANCHUCK
+7  A: 

numbered placeholders will prove to be a PITA when you want to expand your query by adding an additional placeholder in the middle, requiring you to renumber everywhere you use placeholders after the placeholder you just inserted.

Named placeholders, on the other hand, won't have this problem, as the position of the placeholder does not matter for the binding of the placeholder.

alexanderpas