2

I am trying to execute many select statements in mysql in a single query using php.

select * from clients;
select * from users;
select * from products;

Mysql workbench returns only the records of the last sql statement. My PHP code is

$query = "select * from table1;select * from table2;" 
$result = $pdo->prepare($query);
$result->execute();
$rows = $result->fetchAll(PDO::FETCH_NAMED);

I get the error

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other 
unbuffered queries are active.  Consider using PDOStatement::fetchAll().     
Alternatively, if your code is only ever going to run against mysql, 
you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY    
attribute 

Is it possible to execute multiple select queries at once and get all results like MSSQL does?

My only workaround for the moment is to loop through single sql executions and build the response dynamically.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
dev
  • 1,377
  • 1
  • 10
  • 28

1 Answers1

0

Try mysqli::multi_query
http://www.php.net/manual/en/mysqli.multi-query.php

I use it to upload pre-generetad dump files, so it is only CREATE TABLE and INSERT statements, but the manual sais that SELECT works fine two.

And i'm shure that PDO extention has the same method.

Maxim Pavlov
  • 195
  • 1
  • 1
  • 10
  • ..with the caveat that multi_query represents an extreme security vulnerability if the queries are being dynamically built using data from the outside. http://xkcd.com/327/ – Michael - sqlbot Jun 04 '14 at 00:28