PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

PDO Support for Multiple Queries and Driver Specifics

There is a persistent myth in the PHP community that PDO strictly prohibits executing multiple SQL statements within a single transaction or batch operation. While it is true that the default behavior of PDO::query() executes only one statement, this limitation is not inherent to the PDO abstraction layer itself but rather depends on the specific database driver implementation and how the connection is configured.

The confusion often stems from early documentation and the behavior of the default DSN (Data Source Name). When you instantiate a PDO object without specifying a driver prefix like mysql, or when using modern drivers like pdo_mysql, the standard expectation is single-statement execution per call. However, specific legacy drivers such as PDO_MYSQL and PDO_MYSQLND introduced nuances that allowed for multiple queries in certain contexts, primarily through the use of semicolons in the initial query string.

The Legacy Driver Nuance

The source material highlights a critical distinction made by early developers: "In PDO_MySQL there is no such limitation, but you risk to be injected with multiple queries." This statement refers to the specific behavior of the PDO_MYSQL driver (often associated with older PHP versions or specific builds like PECL extensions from that era).

Technically, if you pass a string containing multiple SQL statements separated by semicolons to PDO::query() using these legacy drivers, the driver parses and executes them sequentially. For example:

$pdo = new PDO("mysql:host=localhost;dbname=test", $user, $pass);
// Note: This behavior is highly dependent on the specific driver build and PHP version.
$stmt = $pdo->query("INSERT INTO logs (msg) VALUES ('a'); INSERT INTO logs (msg) VALUES ('b');");

if ($stmt) {
    echo "Multiple queries executed.";
} else {
    echo "PDO::query() only accepts a single statement in this context.";
}

While PDO_MYSQL might technically parse the string, relying on this method is dangerous. As the source material warns, bypassing the separation of concerns between data and logic opens the door to SQL injection vulnerabilities if user input is not rigorously sanitized before concatenation into the query string. Traditional MySQL applications often handled multiple queries by issuing separate calls or using stored procedures; PDO attempted to mimic this flexibility but introduced a security regression if developers assumed query() could safely handle arbitrary multi-statement strings without explicit separation.

The Modern Standard: Multi-Statement Execution

In modern PHP environments, the recommended and safe way to execute multiple queries is not to stuff them into one string, but to chain execution methods or use prepared statements in a loop. The PDO API provides specific methods designed for this purpose that work consistently across PDO_MYSQL, PDO_MYSQLND, and other drivers like PDO_PGSQL.

The most robust approach involves using PDO::exec() for write operations (which return true on success) or iterating through prepared statements. If you require a single object to hold the result of multiple distinct queries, you should utilize PDOStatement execution with explicit separation:

$pdo = new PDO("mysql:host=localhost;dbname=test", $user, $pass);

// Approach 1: Executing statements sequentially
$stmt1 = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt1->execute([1]);

$stmt2 = $pdo->prepare("SELECT COUNT(*) FROM orders");
$result = $stmt2->execute(); // Returns true if successful, no result needed here

// Approach 2: Using exec() for side-effect queries which is often safer for batches
$pdo->exec("UPDATE users SET status = 'active' WHERE id IN (1, 2, 3);");

Security Implications and Best Practices

The warning about PDO_MYSQL being a "more dangerous application" than traditional MySQL applications underscores a significant architectural shift. Traditional MySQL extensions often enforced strict single-statement execution to mitigate injection risks by design. PDO allowed developers to write flexible queries but shifted the burden of security entirely onto the developer's ability to sanitize input or use prepared statements correctly.

If you are working with legacy codebases that utilize PDO_MYSQL and rely on multiple queries in a single query() call, you must refactor them immediately. The modern equivalent is to:

  1. Use PDO::exec() for statements that do not return result sets (like INSERTs or DELETEs).
  2. Use PDO::query() strictly for SELECT statements.
  3. Never concatenate raw user input into a query string intended to contain multiple statements separated by semicolons.

Integration with Modern Frameworks

When integrating this knowledge into larger applications, frameworks like Laravel handle these details internally. Laravel's Eloquent ORM and Query Builder abstract away the need for manual multi-statement handling in ways that are both secure and performant. If you are maintaining a legacy codebase that relies on specific PDO_MYSQL behaviors found in older documentation (such as articles from 2010), it is advisable to migrate to modern drivers and ensure compatibility with current PHP versions, which may have deprecated these multi-statement parsing features for security reasons.

For further reading on securing your application against injection attacks while leveraging PDO's capabilities, resources like https://laravelcompany.com discuss best practices in modern PHP development that align with these architectural shifts. The core takeaway remains: flexibility should never come at the cost of security, and explicit separation of queries is always superior to implicit parsing of multi-statement strings.

Our network