For my database abstraction I want to exclusively rely on PDO parameter markers when using data literals in prepared statements, in order to eliminate the need for escaping and quoting.
The problem is that this seems to work only for some query types: The obvious SELECT, UPDATE and INSERT queries work as expected but CREATE TABLE throws a "General error", regardless of whether I use named or question mark markers.
<?php
// works:
(new PDO('sqlite::memory:'))->prepare('CREATE TABLE test (name TEXT DEFAULT \'unnamed\')')->execute();
// doesn't work:
(new PDO('sqlite::memory:'))->prepare('CREATE TABLE test (name TEXT DEFAULT ?)')->execute(['unnamed']);
This is the error:
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 near "?": syntax error in /home/andy/projects/orm/public/pdo.php:7
Stack trace:
#0 /home/andy/projects/orm/public/pdo.php(7): PDO->prepare()
#1 {main}
thrown in /home/andy/projects/orm/public/pdo.php on line 7
The important difference to Can PHP PDO Statements accept the table or column name as parameter? is that I am not trying to parameterise identifiers but a literal.
If parameters cannot be used in DDL, can someone point me to some docs or specs please?