Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Are RawSql prepared statements emulated or for real?

Hallo, I could not find any tip in the documentation about the PDO adapter prepares the statement.

Let's say I am using MySQL, which supports prepared queries.

Would the command:

$db->execute('INSERT INTO `table1` (?, ?)'), ['data1','data2']) ;

translate into:

'INSERT INTO `table1` ('data1', 'data2')';

or into:

PREPARE stmt FROM 'INSERT INTO `table1` (?, ?)';
SET @a = 'data1';
SET @b = 'data2';
EXECUTE stmt USING @a, @b;
DEALLOCATE PREPARE stmt;

?



64.2k
edited Oct '15

sorry for the spam:

i have no idea, but you can turn the mysql log queries ON and see it there ;-)



289
Accepted
answer

OK: I guess I found the answer, and it is: it depends.

PHP PDO documentation says that PHP emulates prepared statements only when they are not supported by the RBDMS, but actually - after some browsing, it turns out that it emulates it also for MySQL by default.

The trick is to turn emulation off for the PDO connection. That is, after instantiating the adapter $db:

$db->getInternalHandler()->setAttribute( \PDO::ATTR_EMULATE_PREPARES, false )