4

If is posible, how can I fetch two results sets:

$sth=$dbh->prepare("SELECT * FROM tb1 WHERE cond1;
                    SELECT * from tb2 Where cond2");
$sth->execute();
$row=$sth->fetchAll();  print_r ($row);

These are two completely different tables (no fiels in common).

8
  • What for you need to fetch 2 result sets in one statement? Aug 2, 2013 at 19:48
  • #Your Common Sense : Can you please rephrase your question in english.
    – silversky
    Aug 2, 2013 at 19:50
  • @silversky I think it's pretty obvious what he's asking. "Why" do you need to fetch 2 result sets in one statement?
    – Mike
    Aug 2, 2013 at 19:52
  • 1
    Well, because I think that it's better to bring in one 'trip' all the results that you need
    – silversky
    Aug 2, 2013 at 19:56
  • 1
    Yes PDO can fetch two (or more) rowsets. I've given an example in my answer below. Feb 4, 2014 at 11:11

6 Answers 6

13

Yes PDO can fetch two (or more) rowsets, as long as the database you are using supports it. I think MS SQL Server and MySQL both support this functionality, but at the time of writing SQLite does not.

The function you want is PDOStatement::nextRowset

So in your example above, you might do something like;

$sth = $dbh->prepare("SELECT * FROM tb1 WHERE cond1;
                      SELECT * FROM tb2 WHERE cond2");
$sth->execute();
$rowset1 = $sth->fetchAll();
$sth->nextRowset();
$rowset2 = $sth->fetchAll();

print_r($rowset1);
print_r($rowset2);

It's perfectly reasonable for a single stored procedure to return more than one rowset.

2
$rowset[] = $sth->fetchAll(PDO::FETCH_OBJ);
WHILE($sth->nextRowset()) {
    $rowset[] = $sth->fetchAll(PDO::FETCH_OBJ);
}

Now your $rowset will be an array. You may use count() to find out how many rowsets you have. And use foreach loop to get each rowset

1

Depending on the specific version of PHP you're using, you might not even be able to execute multiple statements per query / execute.

The short answer is that no, it is not better to return two separate tables in one query. Both tables would have to have the same amount of columns. There won't be a way of telling result #1 from result #2 in PHP. And if you ever want to change either of the tables, you'll have to ensure that the tables still remain compatible.

You might be able to write a benchmark which runs a couple of thousand queries, first as two separate ones, and then one as UNION ALL (as suggested above). I doubt you're going to find much of a performance difference between the two. If the SQL server is running on the same physical server, the query itself is near instantaneous and you may incur a performance hit for doing the UNION ALL. If it is over the wire, to another server on the network, or over the internet (for whatever reason) that will offset the benefit of the combined query.

If these are semantically separate pieces information, it just isn't a very good idea in the long run to tie the two queries together. This smacks of "premature optimization", and that is the root of all evil as Donald Knuth says.

0
                 $STH = $DBH->prepare('show create table `a`;
                            show create table `b`;
                            show create table `b`;
                        ');
                $STH->execute( );

                do {

                    $row =$STH->fetchAll(\PDO::FETCH_NUM);
                    if($row && count($row)>0){
                        $sql.=$row[0][1] . ';';
                    }
                } while ($STH->nextRowset());

A multi result set in one loop

-1

To answer your question, no, it's not possible how you are doing it. You have two options:

  1. Combine both queries into one
  2. Execute both queries separately and combine their results

Since mysql queries tend to be expensive, I would combine the queries if at all possible. In your case you could use a simple UNION:

SELECT row1, row2, row3
FROM tb1
WHERE id < 10
UNION ALL
SELECT row1, row2, row3
FROM tb2
WHERE id < 10

However you should probably evaluate why you are using two separate tables. If they contain overlapping data, it may be better to combine into one table, if possible.

7
  • 3. Create a sane database to hold all the similar data in one table Aug 2, 2013 at 20:06
  • @YourCommonSense I don't know what his table structure looks like. For all I know, maybe id is the only similar data and there could be a valid reason for doing it in two tables. But yes, I agree with you.
    – Mike
    Aug 2, 2013 at 20:11
  • @YourCommonSense Perhaps you're right. I once designed a system that keeps track of certain changes that are made. Different things can change, so for each type I have it stored in a different table. Some are changes in dates, some are changes of an attribute from one value to another, some are adding/deleting rows. For the ones that are adding rows, I wouldn't want it to have a DATE column because it doesn't apply, so that's why I separated them. Perhaps I need to re-evaluate how it is stored.
    – Mike
    Aug 2, 2013 at 20:20
  • @Mike There two completely different tables and with completely different id.
    – silversky
    Aug 2, 2013 at 20:22
  • @Mike Is it a good idea to Execute both queries separately and combine their results? Do I save time? Or CPU usage?
    – silversky
    Aug 2, 2013 at 20:35
-2

Perhaps you need JOIN if these tables are connected.
Or 2 separate calls if it's completely different and irrelevant tables.

It's impossible to tell more because of your extremely short and obscure question.

Anyway, there is no reason to combine queries only out of desire to run them all in one trip.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.