views:

54

answers:

1

PHP's PDO allows multiple querys to be executed at once, either via the query() method or as a prepared statement. Both of the following examples work:

// Two SQL queries
$query = "SELECT * FROM table; DROP table;"

// Execute via query()
$pdo->query($query);

// Execute via prepared statement
$stmt = $pdo->prepare($query);
$stmt->execute();

Is there any way to limit PDO to a single query at a time, much like the mysql_query() function is?

+2  A: 

Mmm, there's a way of achieving this by disabling the emulation of prepared statements in PDO to make it use the native mysql API instead (multi-querying is not supported in server-side prepared statements):

$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

However, one of the drawbacks of this option is that the query cache is lost.

nuqqsa
"Beginning with 5.1.17, prepared statements use the query cache under certain conditions" - http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html
VolkerK
There's no way to prevent it via the query() method though?
Ben Dowling
Not that I know, unless you use some custom code to previously filter the query string.
nuqqsa