PHP PDOStatement correct placeholder

38 views Asked by At

I would like to know if the PDOStatement object convert the placeholders according the DBMS. In the examples of the documentation we can see just two kinds of placehoders: :named and with question mark, but there is more, with $, for example.

bindValue

bindParam

Does this example show two options as a matter of taste or appropriateness to DBMS support?

1

There are 1 answers

0
Dinu On

PDO will translate the syntax it understands to the query/procedural style of various databases. This is done by every of the databases' driver. While using $ or another placeholder known to a specific database might work (if PDO will just forward the $ it doesn't understand and binding will work), this behavior is undocumented at best, it can stop working at any time. The right way therefore is to use the placeholders PDO understands (the two types documented) and PDO will convert it to the format the DB needs. This is guaranteed to work and is also portable, if at any time you will switch databases.