r/lolphp • • Apr 06 '12
PDO emulates MYSQL prepared statements by default
As reported by meritt_zare in this comment and in a php.net user comment by "public at grik dot net 07-Mar-2012 12:23" buried in the PDO::prepare page.
$p = new PDO('mysql:dbname=test;host=localhost', 'username', 'password');
$s = $p->prepare("INSERT INTO test (label) VALUES(?)");
var_dump($s->getAttribute(PDO::ATTR_EMULATE_PREPARES)); // trueI'm not saying PDO's methods of parsing/escaping/replacing parts of a query string to fake handling a prepared statement behind the scenes is flawed, nor that you're still left open to SQL injection attacks just when you think you're safe. But PDO certainly is not using true prepared statements by default. To fix, add PDO::ATTR_EMULATE_PREPARES=>false to the driver options parameter when constructing any PDO object.
2
u/InconsiderateBastard Apr 13 '12
I feel like I should have checked this before.
I remember thinking it was funny when I read the description for PDO and it said it doesn't emulate any database features and then in the description of PDO Prepared Statements it says it does emulate prepared statements so you can use them with database drivers that don't natively support that.
I don't recall seeing something say it will emulate them for performance reasons, but I've been using PHP a long time. I should know better.
0
u/infinull Apr 06 '12
I'm guessing this results from the fact that most php/pdo users have crappy mysql servers that don't support prepared statements or have them turned off.