1

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?

rplaurindo
  • 1,277
  • 14
  • 23
  • 2
    No, it does not do conversions. PostgreSQL uses `$`, MySQL however uses `?`. PDO with emulated Prepared Statements replaces `?` and `:named` with the (escaped) parameters. Otherwise it will just send the literal string to the DBMS. – Charlotte Dunois Mar 25 '19 at 17:40
  • 1
    `PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility. ` https://www.php.net/manual/en/intro.pdo.php – Charlotte Dunois Mar 25 '19 at 17:42

1 Answers1

2

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.

Dinu
  • 1,374
  • 8
  • 21