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.

3
  • 4
    keep with your "workaround" which is actually regular way of doing things Commented Jun 3, 2014 at 18:54
  • stackoverflow.com/a/6461110/1190388 Commented Jun 3, 2014 at 18:56
  • The method in the above url seems to have SQL injection issues on user input. This is nice for further investigation. Maybe, the manual approach suits better for this purpose Commented Jun 4, 2014 at 7:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

..with the caveat that multi_query represents an extreme security vulnerability if the queries are being dynamically built using data from the outside. xkcd.com/327

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.