php PDO 可以获取两个结果集吗?如果是,那么 1 个结果集或多于 1 个结果集哪个更好?

2022-01-20 00:00:00 set fetch php mysql pdo

如果可能,我如何获取两个结果集:

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

推荐答案

是的 PDO 可以获取两个(或更多)行集,只要您使用的数据库支持它.我认为 MS SQL Server 和 MySQL 都支持此功能,但在撰写本文时 SQLite 不支持.

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.

你想要的函数是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.

相关文章