Original Question:
Despite being aware of PDO's lack of support for executing multiple queries in a single statement, concerns have been raised regarding the availability of support for such functionality in PDO_MYSQL and PDO_MYSQLND.
Answer:
Understanding PDO_MYSQL and PDO_MYSQLND:
PDO_MYSQL was depreciated and replaced by PDO_MYSQLND in PHP 5.3. However, the name remains PDO_MYSQL, making it the default driver for MySQL PDO.
Executing Multiple Queries Using PDO:
To execute multiple queries at once, you require the following:
Using exec:
$sql = " DELETE FROM car; INSERT INTO car(name, type) VALUES ('car1', 'coupe'); INSERT INTO car(name, type) VALUES ('car2', 'coupe'); "; $db->exec($sql);
Note: This method is limited to SQL containing constant values.
Using Statements:
$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"] );
Loop Over Query Results:
Always remember to loop over query results after executing the statement to check for errors or collect the results.
Note on Emulated Prepared Statements:
The above is the detailed content of Does PDO_MYSQL/PDO_MYSQLND Support Multiple Queries in a Single Statement?. For more information, please follow other related articles on the PHP Chinese website!