I do know that PDO does not support multiple queries getting executed in one statement. I've been Googleing and found few posts talking about PDO_MYSQL and PDO_MYSQLND.
PDO_MySQL is a more dangerous application than any other traditional MySQL applications. Traditional MySQL allows only a single SQL query. In PDO_MySQL there is no such limitation, but you risk to be injected with multiple queries.
From: Protection against SQL Injection using PDO and Zend Framework (June 2010; by Julian)
It seems like PDO_MYSQL and PDO_MYSQLND do provide support for multiple queries, but I am not able to find more information about them. Were these projects discontinued? Is there any way now to run multiple queries using PDO.
A quick-and-dirty approach:
Splits at reasonable SQL statement end points. There is no error checking, no injection protection. Understand your use before using it. Personally, I use it for seeding raw migration files for integration testing.
After half a day of fiddling with this, found out that PDO had a bug where...
--
--
--
It would execute the
"valid-stmt1;"
, stop on"non-sense;"
and never throw an error. Will not run the"valid-stmt3;"
, return true and lie that everything ran good.I would expect it to error out on the
"non-sense;"
but it doesn't.Here is where I found this info: Invalid PDO query does not return an error
Here is the bug: https://bugs.php.net/bug.php?id=61613
So, I tried doing this with mysqli and haven't really found any solid answer on how it works so I thought I's just leave it here for those who want to use it..
As I know,
PDO_MYSQLND
replacedPDO_MYSQL
in PHP 5.3. Confusing part is that name is stillPDO_MYSQL
. So now ND is default driver for MySQL+PDO.Overall, to execute multiple queries at once you need:
PDO::ATTR_EMULATE_PREPARES
is set to1
(default). Alternatively you can avoid using prepared statements and use$pdo->exec
directly.Using exec
Using statements
A note:
When using emulated prepared statements, make sure you have set proper encoding (that reflects actual data encoding) in DSN (available since 5.3.6). Otherwise there can be a slight possibility for SQL injection if some odd encoding is used.
Try this function : mltiple queries and multiple values insertion.
Tried following code
Then
And got
If added
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
after$db = ...
Then got blank page
If instead
SELECT
triedDELETE
, then in both cases got error likeSo my conclusion that no injection possible...