Can php PDO fetch two results sets? And If yes, what is better 1 result set or more than 1?

為{幸葍}努か 提交于 2020-03-18 06:49:11

问题


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).


回答1:


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




回答3:


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.




回答4:


                 $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




回答5:


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.




回答6:


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.



来源:https://stackoverflow.com/questions/18025060/can-php-pdo-fetch-two-results-sets-and-if-yes-what-is-better-1-result-set-or-m

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!