As of PHP version 5.3 PDO_MYSQL
driver has been repleaced in favour of PDO_MYSQLND
. It introduced support for multiple queries.
Though, I can't figure out how to get both result sets if more than one SELECT
query has been passed. Both queries have been executed, it can't be that the second one was just dumped.
$db->query("SELECT 1; SELECT 2;")->fetchAll(PDO::FETCH_ASSOC);
Returns:
array(1) {
[0]=>
array(1) {
[1]=>
string(1) "1"
}
}
Both MySQLi and PDO have their advantages: PDO will work on 12 different database systems, whereas MySQLi will only work with MySQL databases. So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries.
You can execute multiple SQL queries with multi_query , a built-in function in PHP. The SQL queries should be in a quoted string to make multiple queries with multi_query , but each SQL should be delimited with a semicolon. For the HTML and CSS, you can use the same HTML and CSS code from the previous section.
PDO::query() returns a PDOStatement object, or FALSE on failure.
As long as you provide all values per ->execute(array( , as above, yes.
It turns out that you need to use PDOStatement::nextRowset
.
$stmt = $db->query("SELECT 1; SELECT 2;");
$stmt->nextRowset();
var_dump( $stmt->fetchAll(PDO::FETCH_ASSOC) );
This will return result for the second query.
It is a bit odd implementation. It would certainly be easier if multi-query statement would just return both results sets under one array. However, the advantage is that this implementation allows to fetch every query using different FETCH styles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With