We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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;

?



85.5k
edited Oct '15

sorry for the spam:

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



578
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 )