Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6054153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:07:16+00:00 2026-05-23T08:07:16+00:00

I do know that PDO does not support multiple queries getting executed in one

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:07:16+00:00Added an answer on May 23, 2026 at 8:07 am

    As I know, PDO_MYSQLND replaced PDO_MYSQL in PHP 5.3. Confusing part is that name is still PDO_MYSQL. So now ND is default driver for MySQL+PDO.

    Overall, to execute multiple queries at once you need:

    • PHP 5.3+
    • mysqlnd
    • Emulated prepared statements. Make sure PDO::ATTR_EMULATE_PREPARES is set to 1 (default for mysql)

    Using exec

    $db = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'root', '');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // works regardless of statements emulation
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 0);
    
    $sql = "
    DELETE FROM car; 
    INSERT INTO car(name, type) VALUES ('car1', 'coupe'); 
    INSERT INTO car(name, type) VALUES ('car2', 'coupe');
    ";
    
    $db->exec($sql);
    

    Note that this method has a limited use and only suitable for SQL that contains constant values. When the data is supplied for SQL from PHP variables, prepared statements must be used istead:

    Using statements

    $db = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'root', '');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // wouldn't work if set to 0. You can comment out this line as 1 is a default
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
    
    $sql = "
    DELETE FROM car; 
    INSERT INTO car(name, type) VALUES (:car1, :type1); 
    INSERT INTO car(name, type) VALUES (:car2, :type2);
    ";
    
    $stmt = $db->prepare($sql);
    $stmt->execute(
        ["car1" => "brand1", "type1" => "coupe", "car2" => "brand2", "type2" => "coupe"]
    );
    // check for errors and collect query results
    do {
        echo $pdo->lastInsertId(); // for example
    } while ($stmt->nextRowset());
    

    Note that in this case you have loop over query results after executing your statement, in order to check for possible errors or collect the query results, as shown above. In case you don’t need to collect any results, the loop can be reduced to just

    while ($stmt->nextRowset());
    

    which will still check for errors (provided PDO::ATTR_ERRMODE is set to PDO::ERRMODE_EXCEPTION as shown above).


    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that you can insert multiple rows at once, is there a way
umm... silly question. I know that one can (and should) use APC cache driver
I know that I need prepared statements because I make more than one call
How does one know when to put variables into the class rather than inside
I know that I can do something like $int = (int)99; //(int) has a
I know that default cron's behavior is to send normal and error output to
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that .NET is JIT compiled to the architecture you are running on
I know that the following is true int i = 17; //binary 10001 int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.